简体   繁体   English

表中具有可枚举模型的MVC模型

[英]MVC Model with enumerable Model in table

I have a model, and in that model I have 2 enumerable items of another model. 我有一个模型,在那个模型中,我有另一个模型的2个可枚举的项目。 Like so: 像这样:

public class InvoiceModel
{
    public int? InvoiceId { get; set; }
    public IEnumerable<ItemModel> Items { get; set; }
    public IEnumerable<ItemModel> AvailableItems { get; set; )
    public decimal? SubTotal { get; set; }
    public decimal? Discounts { get; set; }
    public decimal? Taxes { get; set; }
    public decimal? Total { get; set; }
}

the ItemModel looks like so: ItemModel看起来像这样:

public class ItemModel
{
    public int? Id { get; set; }
    public decimal? Amount { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal? UnitCost { get; set; }
    public int? Quantity { get; set; }
}

Now in the view, the user is creating an invoice, adding Item models (Items) to the invoice based on AvailableItems in a view that looks like a table. 现在,在视图中,用户正在创建发票,并在看起来像表的视图中基于AvailableItems将Item模型(Items)添加到发票中。

So we have a table, where each row represent's items added to the invoice. 因此,我们有一个表格,其中每一行代表添加到发票的项目。 The last row has a drop down list for selection populated by AvailableItems to dynamically add the item they choose from the drop down list to the bottom of the table just before the last row that has the selection for a new item. 最后一行具有一个下拉列表,由AvailableItems填充以供选择,以动态地将他们从下拉列表中选择的项目添加到表底部,紧接在具有新项目选择的最后一行之前。 It also adds the items to the Items property of the model. 还将项目添加到模型的Items属性。 Except I don't know how to achieve this or where to even start asking. 除了我不知道该如何实现,甚至不知道从哪里开始。

I have a mock up of what the table is supposed to look like: 我模拟了该表的外观: 在此处输入图片说明

Any help would be greatly appreciated! 任何帮助将不胜感激!

On the Models side, you're going to need something like this on ItemModel: 在模型方面,ItemModel需要这样的东西:

 public int? InvoiceId { get; set; }
 public int? AvailableInvoiceId { get; set; }

Or you will have mapping issues since the IEnumerable won't know how to link to ItemModel. 否则您将遇到映射问题,因为IEnumerable将不知道如何链接到ItemModel。

However to be honest I think a few design smells have crept in: 不过,老实说,我认为一些设计气味已经悄悄出现:

1) A list of AvailableItems is a curious thing to attach to an invoice, are you sure this shouldn't be a separate table/model? 1)附在发票上的AvailableItems列表是一件奇怪的事,您确定这不应该是单独的表/模型吗? The InvoiceId/AvailableInvoiceId really should be required fields, which won't be possible if you try to stuff two things into the same table. InvoiceId / AvailableInvoiceId实际上应该是必填字段,如果您尝试将两件事填充到同一张表中,则这是不可能的。

2) Primary keys should not be nullable! 2)主键不能为空!

3) I have generally seen navigation properties implemented as ICollection<> rather than IEnumerable<> - you may have issues with this. 3)我通常看到导航属性实现为ICollection <>而不是IEnumerable <>-您可能对此有疑问。

Understandable if this is just a mockup of course. 这当然是可以理解的,这是可以理解的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM