将显示的ViewModel数据与文本框数据一起传递给Controller asp.net mvc 3

时间:2011-08-31 03:42:02

标签: asp.net-mvc-3 binding controller viewmodel

ViewModel类

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; }
}

我的观点

@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,以便在控制器方法中使用?

1 个答案:

答案 0 :(得分:2)

由于您使用Properties

,因此您实际上并未为DisplayFor()发布任何值

如果您只想将其传递给[HttpPost]方法,则需要使用隐藏输入来表示它或使其可编辑。

<td>
    @Html.DisplayFor(modelItem => Model.OrderLines[i].VendorPartNumber)
    @Html.HiddenFor(modelItem => Model.OrderLines[i].VendorPartNumber)
</td>