簡體   English   中英

在asp.net core 2.2中從視圖到控制器發布位於視圖模型內的對象列表

[英]Posting a list of object which is located inside a viewmodel from view to controller in asp.net core 2.2

我有一個名為“RequestGoodsBrandViewModel”的視圖模型,它是兩個模型(Request 和 RequestGoodsBrand)的組合,我想從視圖到控制器發布視圖模型“RequestGoodsBrandViewModel”的對象“RequestGoodsBrand”列表。

 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>

視圖看起來像這樣並且表格是動態的,按加號按鈕將添加新行在此處輸入圖片說明

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

編輯:我使用 ajax 調用將我的 ViewModel 發布到控制器

 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 在此處輸入圖片說明 有什么解決辦法嗎?

我寫了一個相同的演示代碼,請在 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 "";
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM