将显示的 ViewModel 数据与文本框数据一起传递到控制器 asp.net mvc 3

发布于 2024-12-02 03:11:33 字数 2246 浏览 2 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2024-12-09 03:11:33

您实际上并没有发布这些 Properties 的任何值,因为您正在使用 DisplayFor()

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文