第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用jQueryAjax將對(duì)象列表傳遞給MVC控制器方法

使用jQueryAjax將對(duì)象列表傳遞給MVC控制器方法

縹緲止盈 2019-07-25 12:14:25
使用jQueryAjax將對(duì)象列表傳遞給MVC控制器方法我試圖使用jQuery的Ajax()函數(shù)將一個(gè)對(duì)象數(shù)組傳遞給MVC控制器方法。當(dāng)我進(jìn)入PassThing()C#控制器方法時(shí),參數(shù)“Things”為NULL。我嘗試過(guò)使用一種類型的List作為參數(shù),但這也不起作用。我做錯(cuò)什么了?<script type="text/javascript">     $(document).ready(function () {         var things = [             { id: 1, color: 'yellow' },             { id: 2, color: 'blue' },             { id: 3, color: 'red' }         ];         $.ajax({             contentType: 'application/json; charset=utf-8',             dataType: 'json',             type: 'POST',             url: '/Xhr/ThingController/PassThing',             data: JSON.stringify(things)         });     });</script>public class ThingController : Controller{     public void PassThing(Thing[] things)     {         // do stuff with things here...     }     public class Thing     {         public int id { get; set; }         public string color { get; set; }     }}
查看完整描述

2 回答

?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用NickW的建議,我可以用things = JSON.stringify({ 'things': things });這是完整的代碼。

$(document).ready(function () {
    var things = [
        { id: 1, color: 'yellow' },
        { id: 2, color: 'blue' },
        { id: 3, color: 'red' }
    ];      

    things = JSON.stringify({ 'things': things });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Home/PassThings',
        data: things,
        success: function () {          
            $('#result').html('"PassThings()" successfully called.');
        },
        failure: function (response) {          
            $('#result').html(response);
        }
    }); });public void PassThings(List<Thing> things){
    var t = things;}public class Thing{
    public int Id { get; set; }
    public string Color { get; set; }}

我從中學(xué)到了兩件事:

  1. 在Ajax()函數(shù)中,contentType和dataType設(shè)置是絕對(duì)必要的。如果他們失蹤了就沒(méi)用了。經(jīng)過(guò)多次反復(fù)試驗(yàn),我發(fā)現(xiàn)了這一點(diǎn)。

  2. 要將對(duì)象數(shù)組傳遞給MVC控制器方法,只需使用JSON.strgify({‘Things’:Things})格式即可。

我希望這能幫到別人!




查看完整回答
反對(duì) 回復(fù) 2019-07-27
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

你就不能這么做嗎?

var things = [
    { id: 1, color: 'yellow' },
    { id: 2, color: 'blue' },
    { id: 3, color: 'red' }];$.post('@Url.Action("PassThings")', { things: things },
   function () {
        $('#result').html('"PassThings()" successfully called.');
   });

.然后用

[HttpPost]public void PassThings(IEnumerable<Thing> things){
    // do stuff with things here...}



查看完整回答
反對(duì) 回復(fù) 2019-07-27
  • 2 回答
  • 0 關(guān)注
  • 335 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)