2016-04-27 75 views
0

我一直在尋找解決方案,但沒有成功。如何保存JSON對象列表

我已經生成對象的JSON列表:

[ 
    { 
     Email : "[email protected]", 
     Aanwezigheid : "nodig" 
    }, { 
     Email : "[email protected]", 
     Aanwezigheid : "gewenst" 
    }, { 
     Email : "[email protected]", 
     Aanwezigheid : "info" 
    } 
] 

型號:

public class Invitee 
{ 
    public int Id { get; set; } 
    public int AppId { get; set; } 
    public string Email { get; set; } 
    public string Presence { get; set; } 
}; 

jQuery的AJAX調用:

$.ajax({ 
    url: '@Url.Action("NewAppSave","App")', 
    dataType: "json", 
    type: "POST", 
    accept: 'appication/json; charset=utf-8', 
    cache: false, 
    data: jsonData, 
    success: function (data) { 
     if (data.success) { 
      alert("Met succes !"); 
     } 
    }, 
    error: function (xhr) { 
     alert('error'); 
    } 
}); 

什麼是正確的方式來保存這個數據到一個SQL Server數據庫? 所有數據庫字段都與模型中的數據庫完全匹配。

+0

您需要對數據進行字符串化 - 'data:JSON.stringify(jsonData),'但是您的模型不包含名爲'Aanwezigheid'的屬性。 –

+0

修改您的js對象以包含'Presence'而不是'Aanwezigheid'。例如。 { 電子郵件:「[email protected]」, 存在:「nodig」 } –

+0

您需要一個「實體數據模型」。可以將整個json序列化並作爲字符串存儲在表列中。 – Sajal

回答

0

你需要通過你的JSON模式,C#創建一個相同的模型,然後循環,並添加到相應的C#模型

0

你的JS對象

var jsonData = [ 
    { 
     Email : "[email protected]", 
     Presence : "nodig" 
    }, { 
     Email : "[email protected]", 
     Presence : "gewenst" 
    }, { 
     Email : "[email protected]", 
     Presence : "info" 
    } 
] 

動作方法:

public ActionResult NewAppSave(List<Invitee> invitees) 
{ 
    //use the list data of employees to save this data into db 
} 

在阿賈克斯調用,使用stringify

data: JSON.stringify(jsonData)