张贴在ASP.NET MVC 4的模型视图模型的列表 [英] Posting a list of view models in a model in ASP.NET MVC 4

查看:99
本文介绍了张贴在ASP.NET MVC 4的模型视图模型的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试后与包含的型号列表模型的视图,但在发帖时,只有在车型列表中的第一个模型显示其ID。我现在用的editorFor通过编辑模板来显示我的型号。在模板通知我有ID值的hiddenFor。为什么当我发布的形式,它仅显示在该列表中的第一个模型?我该如何解决这个问题,这样的帖子将包含的型号列表中的所有ID值被张贴?它能够为下拉菜单莫名其妙发布值全部。

So I am trying to post a view with a model that contains a list of models, but when posting, only the first model in the list of models show their Id. I am using the editorFor to show my models through the editor template. Notice in the template I have the hiddenFor for the Id value. Why when i post the form it only shows the first model in the list? How do I fix this so that the post will contain all the Id values in the list of models being posted? Its able to post the values for the drop downs somehow for all of them.

主视图:

@using (Html.BeginForm("OrderPost", "Order", FormMethod.Post))
 {
            <table id="orders">
                    <thead>
                        <tr>
                            <th>
                            </th>
                            <th>
                                <b>ID</b>
                            </th>
                            <th>
                                <b>Product Name</b>
                            </th>
                            <th>
                                <b>SKU</b>
                            </th>
                            <th>
                                <b>Quantity</b>
                            </th>
                            <th>
                                <b>Price</b>
                            </th>
                            <th>
                                <b>Sub Total</b>
                            </th>
                        </tr>
                    </thead>
                    @Html.EditorFor(x => x.LineItemModels)

            </table>
            <button type="submit"> submit</button>
}

编辑模板:

@model Project.Models.LineItemModel
@Html.HiddenFor(x => x.Id)
<tr>
    <td>
        @if (!Model.HideSelected) { 
            @Html.CheckBoxFor(x => x.Selected)
        }
        else
        {
            <span>Returned</span>
        }
    </td>
    <td>
        @Model.Id
    </td>
    <td>
        @Model.Name
    </td>
    <td>
        @Model.SKU
    </td>
    <td>
        @if (!Model.HideSelected) {
            @Html.DropDownListFor(x=>x.QuantitySelected, Model.QuantityToRefund)
        }
        else
        {
            <span>0</span>
        }
    </td>
    <td id="subTotal @Model.Id" class="subTotal"></td>
</tr>

型号:

public class OrderModel
{
    public List<LineItemModel> LineItemModels { get; set; }
    public string Comments { get; set; }
    public int OrderId { get; set; }
}

![此处输入的形象描述] [1]

![enter image description here][1]

(试图帖子的ID的调试模式图像只显示在型号[0],但我需要的10代表我猜...)

(tried to post image of debug mode of the Id only showing in Model[0] but i need a rep of 10 i guess...)

推荐答案

我认为你可以使用JSON格式(透过JSON.stringify())将数据发布到服务器,
使用jQuery你可以建立你的数组

I think that you can use JSON format (using JSON.stringify()) to post data to the server, with JQuery you can build your array

var model = {};
model.field1 = $('#field1').val(); 
model.field2 = $('#field2').val();
model.field3 = ...

var _data = {};
_data.OrderId = 100;
_data.Comments = 'Some comments';
_data.LineItemModels = BuildArray(); //BuildArray return an array of model objects in your form that most have the LineItemModel structure


$.ajax({
   url: yourUrl,
   type: "POST",
   cache: false,
   data: JSON.stringify(_data),
   dataType: "json",
   contentType: 'application/json',
   success: function (data) {},
   error: function (request) {}
});

这篇关于张贴在ASP.NET MVC 4的模型视图模型的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆