简体   繁体   English

在MVC 5中,如何将表对象数据作为列表传递给Controller View Model?

[英]In MVC 5 how to pass table Object data as a list to Controller View Model?

My Controller ItemRequestViewModel is null & the issue is it didn't pass any value from view to controller but when i pass a List<string> it shows one Row Value. 我的控制器ItemRequestViewModel为null,问题是它没有从视图传递任何值到控制器,但是当我传递List<string>它显示一个行值。 I just can't pass multiple object to Controllers ViewModel. 我只是不能将多个对象传递给Controllers ViewModel。 i have tried by passing as a List<ItemRequestViewModel reqItem> as well but it seems no hope. 我也尝试通过作为List<ItemRequestViewModel reqItem>传递,但似乎没有希望。

My Controller 我的控制器

public ActionResult ReqNotifyApprove(List<ItemRequestViewModel> reqItem)
{
   return null;
}

My View 我的观点

@model IEnumerable<Management_System.ViewModels.ItemRequestViewModel>
@using (Html.BeginForm("ReqNotifyApprove", 
                       "RequestedItems", 
                       FormMethod.Post, 
                       new { id = "ReqItemApproveForm" }))
 {
    <table class="table table-bordered results">
        <thead>
            <tr>
                <th>ID</th>
                <th>ITEM ID</th>
                <th>DESCRIPTION</th>
                <th>UOM</th>
                <th>AVL QTY</th>
                <th>REQ QTY</th>
                <th>AUTH QTY</th>
            </tr>
        </thead>
        <tbody>
        @foreach (var item in Model)
        {
        <tr>
            <th>
                @Html.DisplayFor(modelItem => item.Id)
                @Html.HiddenFor(modelItem => item.Id, new { name = "Id" })
            </th>
            <th>
                @Html.DisplayFor(modelItem => item.PDT_CODE)
            </th>
            <td>
                @Html.DisplayFor(modelItem => item.PDT_NAME)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UOM_SNAME)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.AvailableQty)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ISS_QTY)
            </td>
            <td>
                <input type="number" id="ApprovedQuantity" name="ApprovedQuantity" class="form-control" min="0" style="width: 80px;">
            </td>
        </tr>
        }
        </tbody>
    </table>
<div>
    <input class="btn btn-lg btn-success" type="submit" value="Approve" />
</div>
}

I have already tried different Data binding as well but no hope. 我已经尝试过不同的数据绑定,但是没有希望。

The problem on your approach, is the name attribute that you fixed set to "Id". 您的方法遇到的问题是您固定设置为“ Id”的名称属性。 The way MVC binds a collection, is by adding "[index]" as a prefix to the HTML name attribute. MVC绑定集合的方式是在HTML名称属性中添加“ [index]”作为前缀。 You can find more details about it here - https://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ 您可以在这里找到有关它的更多详细信息-https: //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

So, if you remove this new { name = "Id" } , and use a for instead of foreach , your Razor will be something like: 因此,如果删除此new { name = "Id" } ,并使用for代替foreach ,则剃刀将类似于:

@for (int i=0; i < Model.Count; i++)
{
    @Html.DisplayFor(modelItem => Model[i].Id)
    @Html.HiddenFor(modelItem => Model[i].Id)
    ....
}

And the result of the hidden input should be something like: 隐藏输入的结果应类似于:

<input type='hidden' name="[0].Id" id="0__Id" />
<input type='hidden' name="[1].Id" id="1__Id" />
<input type='hidden' name="[2].Id" id="2__Id" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM