[英]ASP.NET MVC4 return list<T> from view back to model
I am new to ASP.NET MVC and have the below situation. 我是ASP.NET MVC的新手,具有以下情况。 I need some help creating the proper model or view. 我需要一些帮助来创建正确的模型或视图。
I have the below model at the moment. 我目前有以下型号。
public class ExtrasModel
{
public List<ExtraItem> ExtraItems { get; set; }
public DateTime FlightArrivalTime { get; set; }
public PickupInfoType PickupInfo {get; set; }
}
public class ExtraItem
{
public string ExtraInfo { get; set; }
public int ProdExtraId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Cost { get; set; }
public int CostType { get; set; }
public string Currency { get; set; }
public int Quantity { get; set; }
public double TotalCost { get; set; }
}
List of extras will be fetched from the database. 额外物品清单将从数据库中获取。
This is how I have the View : 这就是我的观点:
@foreach(var extra in Model.ExtrasList)
{
<tr>
<td>
//Not sure what code to put here
@Html.DropDownListFor(p => p.ExtrasList, new SelectList(listItems, "Value", "Text", extra.Quantity), new { id = extra.ProdExtraId })
</td>
<td>@extra.Name</td>
<td>@string.Format("{0} {1}", extra.Cost, extra.Currency)</td>
</tr>
}
Now how to make the view return the model with the quantities selected for extras? 现在如何使视图返回带有额外选择数量的模型?
Thanks in advance!! 提前致谢!!
Create an EditorTemplate for the ExtraItem
Model. 为ExtraItem
模型创建一个EditorTemplate 。 Content of the Editor Template is basically the same like the content of your loop. 编辑器模板的内容与循环的内容基本相同。
Replace the Loop with the following statement: 用以下语句替换循环:
@Html.EditorFor(x => x.ExtraItems)
This should do it. 这应该做。
If you are going to put the elements in a form, that will be postback to the server, then you can create a custom editor template for your type. 如果要将元素放入表单中,该表单将回发到服务器,则可以为类型创建自定义编辑器模板。 If you do this, then use Html.EditorFor with your enumerable. 如果这样做,则将Html.EditorFor与您的枚举一起使用。
Create a EditorTemplates folder in your view folder, then add a new strongly typed partial view. 在视图文件夹中创建一个EditorTemplates文件夹,然后添加一个新的强类型部分视图。 Name it ExtraItem. 将其命名为ExtraItem。 Put the markup in this view for each individual item in your enumerable. 将此枚举中每个单独项目的标记放入此视图中。
When you use: 使用时:
@Html.EditorFor(x=> x.ExtraItems)
You will get output like this: 您将获得如下输出:
<input id="ExtraItems_0__name" name="ExtraItems[0].name" type="hidden" value="aName">
Notice the array style name. 注意数组样式名称。 When the form is posted back the server will recognise the enumerable correctly. 将表格回发后,服务器将正确识别可枚举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.