【问题标题】:Model property concatenated from properties in different models由不同模型中的属性连接的模型属性
【发布时间】:2019-06-13 14:37:36
【问题描述】:

我首先使用代码在数据库中创建实体,主要是指字典表:

public class Item
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Material Material { get; set; }
    public int MaterialId { get; set; }
    public Supplier Supplier { get; set; }
    public int SupplierId { get; set; }
    public Charact Charact { get; set; }
    public int CharactId { get; set; }
    public ItemType ItemType { get; set; }
    public int ItemTypeId { get; set; }
    public Size Size { get; set; }
    public int SizeId { get; set; }
    public int GroupId { get; set; }
    public Group Group { get; set; }   
    public string Number { get; set; }
    public string DescriptionSum { get; set; }
}

所有字典模型都具有相似的属性,其中属性'No'在这种情况下是关键属性,并且是一个或两个字符串:

public class Size
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string No { get; set; }
}

在创建新项目时,用户将从字典表中所有可用的记录中选择一条记录并将它们保存在项目表中:

用于创建的 ItemsController:

// GET: Items/Create
public IActionResult Create(int? gr)
{
    ViewData["CharactId"] = new SelectList(_context.Charact, "Id", "Name");
    ViewData["GroupId"] = new SelectList(_context.Group, "Id", "Name");
    ViewData["ItemTypeId"] = new SelectList(_context.ItemType.Where(ItemType => ItemType.GroupId == gr), "Id", "Name");
    ViewData["MaterialId"] = new SelectList(_context.Materials, "Id", "Name");
    ViewData["SizeId"] = new SelectList(_context.Size, "Id", "Name");
    ViewData["SupplierId"] = new SelectList(_context.Supplier.Where(Supplier => Supplier.GroupId == gr), "Id", "Name");
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Name,Description,MaterialId,SupplierId,CharactId,ItemTypeId,SizeId,GroupId,Number,DescriptionSum")] Item item)
{
    if (ModelState.IsValid)
    {
        _context.Add(item);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    ViewData["CharactId"] = new SelectList(_context.Charact, "Id", "Name", item.CharactId);
    ViewData["GroupId"] = new SelectList(_context.Group, "Id", "Name", item.GroupId);
    ViewData["ItemTypeId"] = new SelectList(_context.ItemType, "Id", "Name", item.ItemTypeId);
    ViewData["MaterialId"] = new SelectList(_context.Materials, "Id", "Name", item.MaterialId);
    ViewData["SizeId"] = new SelectList(_context.Size, "Id", "Name", item.SizeId);
    ViewData["SupplierId"] = new SelectList(_context.Supplier, "Id", "Name", item.SupplierId);
    return View(item);
}

我要做的是“自动”填充 Number 属性,根据表单选项中的选择,基于“否”属性并将其保存在数据库中:

Number = Group.No + Supplier.No + ItemType.No + Charact.No + Material.No + Size.No;

连接数字将定义选择的选项配置。因为同一个“No”可以在同一张表中出现多次,所以不能作为标识列。


我尝试了几种方法,我在网上找到了:

更新 ItemController:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult>Create([Bind("Id,Name,Description,MaterialId,SupplierId,CharactId,ItemTypeId,SizeId,GroupId,Number,DescriptionSum")] Item item)
{            
    if (ModelState.IsValid)
    {
        item.Number = item.Group.No + item.Supplier.No + item.ItemType.No + item.Charact.No + item.Material.No + item.Size.No;
        _context.Add(item);
        await _context.SaveChangesAsync();
    }
ViewData["CharactId"] = new SelectList(_context.Charact, "Id", "Name", item.CharactId);
ViewData["GroupId"] = new SelectList(_context.Group, "Id", "Name", item.GroupId);
ViewData["ItemTypeId"] = new SelectList(_context.ItemType, "Id", "Name", item.ItemTypeId);
ViewData["MaterialId"] = new SelectList(_context.Materials, "Id", "Name", item.MaterialId);
ViewData["SizeId"] = new SelectList(_context.Size, "Id", "Name", item.SizeId);
ViewData["SupplierId"] = new SelectList(_context.Supplier, "Id", "Name", item.SupplierId);
return View(item);
}

发生异常:

处理请求时发生未处理的异常。 NullReferenceException:对象引用未设置为对象的实例。 Database.Controllers.ItemsController.Create(Item item) 在 ItemsController.cs 中

item.Number = item.Group.No + item.Supplier.No + item.ItemType.No + item.Charact.No + item.Material.No + item.Size.No;

改变模型:

public class Item
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Material Material { get; set; }
    public int MaterialId { get; set; }
    public Supplier Supplier { get; set; }
    public int SupplierId { get; set; }
    public Charact Charact { get; set; }
    public int CharactId { get; set; }
    public ItemType ItemType { get; set; }
    public int ItemTypeId { get; set; }
    public Size Size { get; set; }
    public int SizeId { get; set; }
    public int GroupId { get; set; }
    public Group Group { get; set; }   
    public string Number 
    { get
                           { 
                   return this.Number = this.Group.No + this.Supplier.No + this.ItemType.No + this.Charact.No + this.Material.No + this.Size.No; 
               } 
               private set { }
             }
    public string DescriptionSum { get; set; }
}

相同的例外,但与行有关

public string Number { get { return this.Number = this.Group.No + this.Supplier.No + this.ItemType.No + this.Charact.No + this.Material.No + this.Size.No; } private set { } }

在项目模型中。

其他型号变更:

public class Item
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Material Material { get; set; }
    public int MaterialId { get; set; }
    public Supplier Supplier { get; set; }
    public int SupplierId { get; set; }
    public Charact Charact { get; set; }
    public int CharactId { get; set; }
    public ItemType ItemType { get; set; }
    public int ItemTypeId { get; set; }
    public Size Size { get; set; }
    public int SizeId { get; set; }
    public int GroupId { get; set; }
    public Group Group { get; set; }
    private string _value;
    public string Number { get { return _value; } private set {_value = this.Group.No + this.Supplier.No + this.ItemType.No + this.Charact.No + this.Material.No + this.Size.No; } }
    public string DescriptionSum { get; set; }       
}

还是一样的异常:

public string Number { get { return _value; } private set {_value = this.Group.No + this.Supplier.No + this.ItemType.No + this.Charact.No + this.Material.No + this.Size.No; } }

我找不到任何其他解决方案。请帮忙。

BR

【问题讨论】:

  • 导航属性为空,使用前需要创建实例
  • 当你创建 Item 类的实例 Material 类 & The Charact 类为空。因此,当您尝试访问属性 Number 时,它会抛出对象引用
  • public string Number { get { return _value; } private set {_value = this.Group?.No + this.Supplier?.No + this.ItemType?.No + this.Charact?.No + this.Material?.No + this.Size?.No; } }

标签: c# asp.net-mvc asp.net-core


【解决方案1】:

当您创建Item class 的实例时; material 类和 Charact 类为空。因此,当您尝试访问 Number 时,它将抛出对象引用,因为 Navigation Properties 为 null。

选项 1 检查 Null 并分配变量。

public string Number 
{ 
  get { return _value; } 
  private set {_value = this.Group?.No + this.Supplier?.No + this.ItemType?.No + 
   this.Charact?.No + this.Material?.No + this.Size?.No; } 
}

如果您使用 C# 6 或更高版本,您可以使用 Null-Conditional Operators

选项 2 用数据填充 Navigation 对象。

public class Item
{
  //YourProperties Here

  public Item()
  {
     this.Material = new Material();
     this.Charact = new Charact();
  }
}

基本上,您需要数据才能将数据分配给导航属性,然后它将与您的相同代码一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    相关资源
    最近更新 更多