简体   繁体   中英

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

I have a view model called "RequestGoodsBrandViewModel" and it's the combination of two models (Request and RequestGoodsBrand), I want to post a list of objects "RequestGoodsBrand" of view model " RequestGoodsBrandViewModel " from view to controller.

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

my view

                                <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 looks like this and table is dynamic,by pressing plus button new row will be added在此处输入图片说明

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

Edit: I post my ViewModel to the controller using ajax call

 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) {

            }
        });

the first object receives data but the second object which is a list, return null 在此处输入图片说明 Any solution?

i wrote a demo code for the same please on Jquery try it :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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