簡體   English   中英

如何將多個 json 數組從 ajax 傳遞到 controller?

[英]How to pass multiple json array to controller from ajax?

我有兩個 json 陣列。 我正在嘗試將這些 json 陣列發送到我的 controller。 當我想發送其中一個時,一切都很好,但我無法發送第二個。 我該如何解決?

從視圖中發布 function

function HobiIlgiAlanKaydetGuncelle() {

    var hobiler = $('#Hobiler').val(); // Json array of object
    var ilgiAlanlar = $('#IlgiAlan').val();

    $.ajax({
        url: "/Kullanici/HobiVeIlgiAlanlariniKaydetGuncelle",
        type: "POST",
        contentType: 'application/json; charset=UTF-8',
        dataType: 'json',
        data: {hobiler : hobiler,ilgiAlanlar : ilgiAlanlar},
        success: function (response) { }

    });
}

Controller

[HttpPost]
public async Task<JsonResult> HobiVeIlgiAlanlariniKaydetGuncelle([FromBody] List<HobilerVM> hobiler, List<IlgiAlanlarVM> ilgiAlanlar)
{
   //When I put second parameter, both of them comes with null    
}

霍比勒虛擬機

public class HobilerVM
{
    public int id { get; set; }
    public string value { get; set; }
}

伊爾吉艾倫VM

public class IlgiAlanVM
{
    public int id { get; set; }
    public string value { get; set; }
}

問題在於以下行:

data: {hobiler : hobiler,ilgiAlanlar : ilgiAlanlar}

這是 javascript 中的 object。 c# 中的等效項應為:

public class MyData {
    public List<HobilerVM> hobiler;
    public List<IlgiAlanlarVM> ilgiAlanlar;
}

然后在您的 controller 中:

    [HttpPost]
    public async Task<JsonResult> HobiVeIlgiAlanlariniKaydetGuncelle([FromBody] MyData data)
    {
       //When i put second parameter, both of them comes with null    
    }

有關更多信息,請查看為什么 ASP.NET Web API 只允許一個參數用於 POST 方法?

Web API doesn't allow you to pass multiple complex objects in the method signature of a Web API controller method — you can post only a single value to a Web API action method. 這個值甚至可以是一個復雜的 object。 通過將一個參數映射到實際內容並通過查詢字符串將其余參數映射到 POST 或 PUT 操作,可以傳遞多個值。 參考: 如何將多個參數傳遞給Web API controller方法以及WebAPI如何進行參數綁定

解決方案

public class ComplexObject {
   property List<HobilerVM> Hobiler { get; set; }
   property List<IlgiAlanlarVM> IlgiAlanlar { get; set; }
}

[HttpPost]
public async Task<JsonResult> HobiVeIlgiAlanlariniKaydetGuncelle([FromBody] ComplexObject data)
{
 //When i put second parameter, both of them comes with null    
}

快樂的編碼,干杯!

我試過你的代碼,它對我來說很好,除了一些事情。

首先,您的第二個參數對您在代碼上發布的內容具有不同的 ViewModel:

public class IlgiAlanVM
{
   public int id { get; set; }
   public string value { get; set; }
}

但是在您的參數上,您使用的是不同的 ViewModel:

([FromBody] List<HobilerVM> hobiler, List<IlgiAlanlarVM> ilgiAlanlar)

正如您在此處看到的, IlgiAlanVMList<IlgiAlanlarVM>上有所不同

其次,我只是使用了相同的代碼,但沒有[FromBody] 所以那將是:

//I already edited your List of Model here on the second parameter from IlgiAlanVM to IlgiAlanlarVM
[HttpPost]
public async Task<JsonResult> HobiVeIlgiAlanlariniKaydetGuncelle
                          (List<HobilerVM> hobiler, List<IlgiAlanlarVM> ilgiAlanlar) 

最后,我只是確保它是一個對象數組,以確保它能夠很好地綁定到您的模型列表中:

        var hobiler = [{
            id: 1,
            value: 'My Value'
        }];
            
        var ilgiAlanlar = [{
            id: 1,
            value: 'MyValue'
        }];

       $.ajax({
            url: '@Url.Action("HobiVeIlgiAlanlariniKaydetGuncelle", "Kullanici")',
            type: "POST",
            contentType: 'application/json; charset=UTF-8',
            dataType: 'json',
            data: JSON.stringify({ hobiler : hobiler, ilgiAlanlar : ilgiAlanlar }),
            success: function (response) { }
        });

暫無
暫無

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

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