javascript - 在asp.net core 2.2中将位于 View 模型内的对象列表从 View 发布到 Controller

标签 javascript jquery asp.net-core

我有一个名为“RequestGoodsBrandViewModel”的 View 模型,它是两个模型(Request 和 RequestGoodsBrand)的组合,我想将 View 模型“RequestGoodsBrandViewModel”的对象“RequestGoodsBrand”列表从 View 发布到 Controller 。

 public class RequestGoodsBrandViewModel
{
    public Request request{ get; set; }//single object of this
    public List<RequestGoodsBrand> requestGoodsBrand { get; set; }//List of object of this 
}

我的看法

                                <tbody>
                                <tr v-for="(data, index) in goods">
                                    <td width="20%">
                                        <select id="goodsDDL" class="form-control">
                                            <option v-for="options in goodsList" :value="options.id">{{options.name}}</option>
                                        </select>
                                    </td>
                                    <td width="20%">
                                        <input type="number" v-model="data.quantity" id="quantityTxt" class="form-control" />
                                    </td>
                                    <td width="20%">

                                        <select id="brandDDL" class="form-control">
                                                <option v-for="options in brands" :value="options.id">{{options.name}}</option>
                                            </select>
                                    </td>
                                    <td width="7%">
                                        <a v-on:click="addRow(index)" class="btn"><i class="fa fa-plus-circle" style="color:darkgreen"></i></a>
                                        <a v-on:click="removeRow(index)" class="btn"><i class="fa fa-minus-circle" style="color:red"></i></a>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="4">
                                        <label>توضیحات</label>
                                        <textarea id="descTxt" class="form-control"></textarea>
                                    </td>
                                </tr>
                            </tbody>

View 看起来像这样,并且表格是动态的,通过按加号按钮将添加新行 enter image description here

 public IActionResult AddRequestedGoods(RequestGoodsBrandViewModel model)// only RequestGoodsBrand of RequestGoodsBrandViewModel must  be a list 
    {
    }

编辑: 我使用 ajax 调用将 ViewModel 发布到 Controller

 var requestGoodsBrand = {}
        var request = {
            NeedDate: $('#needDate').val(),
            Description: $('#descTxt').val(),
        }
        var table= document.getElementById('goodsTbl')
        for (var i = 1; i < table.rows.length - 1; i++) {
            requestGoodsBrand[i] = {
            GoodsId:(table.rows[i].cells[0].children[0].value),
            Quantity:(table.rows[i].cells[1].children[0].value),
            BrandId:(table.rows[i].cells[2].children[0].value)
            }
        }
        var model = {
            "request": request,
            "requestGoodsBrand": requestGoodsBrand,
        }
        console.log(model)
                    var data = JSON.stringify(model)
        $.ajax({
            type: "POST",
            async: true,
            url: '/GoodsRequest/AddRequestedGoods',
            data: model,
            //contentType: "application/json; charset=utf-8",
            //dataType: "html",
            success: function (data) {

            }
        });

第一个对象接收数据,但第二个对象是列表,返回 null enter image description here 有什么解决办法吗?

最佳答案

我写了一个演示代码,请在 Jquery 上尝试一下:

Jquery

function () {
            var requestGoodsBrand = [];
        var request = {
            NeedDate: 'demo1',
            Description: 'demo2',
            }
            $('table tr').each(function () {
                var entity = {
                    GoodsId: $($(this).find('td:eq(0) input')).val(),
                    Quantity: $($(this).find('td:eq(1) input')).val(),
                    BrandId: $($(this).find('td:eq(2) input')).val()
                };
                requestGoodsBrand.push(entity);
            })
            $.post('/Home/AddRequestedGoods', { requestGoodsBrand: requestGoodsBrand, request: request })
                .done(function (result) {

                }

C#

 [HttpPost]
        public string AddRequestedGoods(Request request, List<RequestGoodsBrand> requestGoodsBrand)
        {
            return "";
        }

关于javascript - 在asp.net core 2.2中将位于 View 模型内的对象列表从 View 发布到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59907464/

相关文章:

javascript - jQuery:如何将效果应用于与其他元素共享名称的元素

javascript - 为什么 Node.js 会有这样的行为?

c# - services.Configure<>() 还是 services.AddSingleton().Get()?

visual-studio - 在 Visual Studio 2015 中启动 IIS Express session 后如何运行 gulp 任务

c# - 为什么 Azure Active Directory 身份验证会在我的 ASP.NET Core 应用程序中导致字符编码错误?

javascript - JS & Node : Read table from DB with AJAX and display in table

javascript - Bootstrap 4 表格,可滚动主体和标题固定

ajax - 如何检测 ajax 调用的开始?

jquery - CSS3 + jQuery 翻页时间调整

jquery - 滚动条看起来就像在 Azure 管理门户中一样