首页 > 解决方案 > 由不同模型中的属性连接的模型属性

问题描述

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

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”可以在同一张表中出现多次,所以不能作为标识列。


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

更新项目控制器:

[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

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

解决方案


当您创建实例时Item classmaterial类 & The为Charact空类。因此,当您尝试访问时,Number它会抛出对象引用,因为导航属性为空。

选项 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();
  }
}

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


推荐阅读