ViewModel class
public class CreateMRViewModel
{
public Decimal? Freight { get; set; }
public IList<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
public string LineNumber { get; set; }
public string VendorPartNumber { get; set; }
public long? Quantity { get; set; }
public string UOM { get; set; }
public decimal PricePerUom { get; set; }
public long? ReturnQuantity { get; set; }
public long? QuantityAvaiToReturn { get; set; }
}
My View
@model CreateMRViewModel
@using (Html.BeginForm()) {
<fieldset>
<table>
<tr>
<th>Line Number</th>
<th>Part Number</th>
<th>UOM</th>
<th>PricePerUom</th>
<th>Available Qty</th>
<th>Return Qty</th>
</tr>
@for (int i = 0; i < Model.OrderLines.Count; i++)
{
<tr>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].LineNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].VendorPartNumber)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].UOM)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].PricePerUom)</td>
<td>@Html.DisplayFor(modelItem => Model.OrderLines[i].Quantity)</td>
<td>@Html.TextBoxFor(modelItem => Model.OrderLines[i].ReturnQuantity)</td>
</tr>
}
</table>
<br />
<div class="display-label">@Html.LabelFor(model => Model.Freight)</div>
<div class="display-field">@Html.TextBox("Freight", new Int64())</div>
<br />
<br />
<input class="submitForm" type="submit" value="Create Return" />
</fieldset>
}
动作控制器
[HttpPost]
public ActionResult CreateMR(CreateMRViewModel mrViewModel)
{
//some code here
}
我的问题是:我得到的CreateMRViewModel控制器,但只有文本框值可供我使用。我怎样才能得到显示的值,如LineNumber或VendorPartNumber用于控制器方法?
由于您使用的是DisplayFor()
Properties
的任何值。如果你只是想把它传递给[HttpPost]
方法,你需要用一个隐藏的输入来表示它,或者使它可编辑。
<td>
@Html.DisplayFor(modelItem => Model.OrderLines[i].VendorPartNumber)
@Html.HiddenFor(modelItem => Model.OrderLines[i].VendorPartNumber)
</td>