开发者

Passing displayed ViewModel data along with textbox data to Controller asp.net mvc 3

开发者 https://www.devze.com 2023-03-31 17:50 出处:网络
ViewModel class public class CreateMRViewModel { public Decimal? Freight { get; set; } public IList<OrderLine> OrderLines { get; set; }

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].U开发者_开发知识库OM)</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>
}

Action Controller

[HttpPost]
public ActionResult CreateMR(CreateMRViewModel mrViewModel)
{
    //some code here
}

My problem is: I get the CreateMRViewModel to controller but only textbox values are available for me to use. How can I get the displayed values such as LineNumber or VendorPartNumber for use in controller method?


You're not actually posting any values for those Properties since you're using DisplayFor()

If you simply want to just pass it through to the [HttpPost] method you're going to need to represent it with a hidden input or make it editable.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号