The issue that you are facing is related to the names generated by the ICollection<T>
controls. Here is a detailed discussion by Phil Haack and a solution by him (in terms of an @Html extension method; download the sample project from the link given at the end of his blog post). This post targets MVC/MVC2; however it is still applicable with MVC3.
Alternatively if you don't want to follow the hack, you can opt for a EditorTemplate
for your OrderLine
entity model.
Here are the steps.
1) Create Editor template under (Views ->Shared -> EditorTemplates -> OrderLine.cshtml
)
It is important to create a folder named EditorTemplates
under Shared
, and the template name should be same as the EntityModel for which you want to create the templete; hence the name OrderLine.cshtml
)
2) Code for OrderLine.cshtml
@model OrderTracker.Models.OrderLine
@{
Layout = null;
}
<!DOCTYPE html>
@Html.HiddenFor(modelItem => Model.id)
<tr>
<td>
@Html.EditorFor(modelItem => Model.Description)
</td>
<td>
@Html.EditorFor(modelItem => Model.Quantity)
</td>
<td>
@Html.EditorFor(modelItem => Model.Weight)
</td>
<td>
@Html.EditorFor(modelItem => Model.Price)
</td>
</tr>
3) Edit your View with this code (note that I've used EditorFor
for OrderLines collection)
@model OrderTracker.Models.Order
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
<fieldset>
<legend>Order</legend>
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.UserId)
<div>
@Html.LabelFor(model => model.OrderDate)
</div>
<div>
@Html.EditorFor(model => model.OrderDate)
</div>
<div>
@Html.LabelFor(model => model.Description)
</div>
<div>
@Html.EditorFor(model => model.Description)
</div>
<div>
<table>
<tr>
<th>
Description
</th>
<th>
Quantity
</th>
<th>
Weight
</th>
<th>
Price
</th>
</tr>
@Html.EditorFor(model => model.OrderLines)
</table>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
4) Now on post back you will see the values
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…