由不同模型中的属性串联而成的模型属性

时间:2019-06-13 06:12:22

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

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

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;

级联编号将定义选项的选定配置。由于相同的“否”可以在同一张表中多次出现,因此不能用作标识列。


我尝试了几种方法,这些方法可以在Web上找到:

更新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

1 个答案:

答案 0 :(得分:0)

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

选项1 选中“空”并分配变量。

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 。在导航对象中填充数据。

public class Item
{
  //YourProperties Here

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

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