[英]send Data to controller from Jquery. MVC C#
我的jQuery腳本代碼部分,腳本運行良好,並在dataBLL中設置了數據數組/對象。
var dataBLL = [];
$('#mytable tr').each(function (i) {
dataBLL.push({
id: $(this).find('td:eq(0)').text(),
ctype: $(this).find('td:eq(1)').text(),
cpath: $(this).find('td:eq(2)').text(),
ckey: $(this).find('td:eq(3)').text(),
ckey: $(this).find('td:eq(4) input:frist').val(),
});
$.ajax ({
url:"User/BllBtn",
type:"POST",
data:"dataBll="JSON.stringify(dataBLL);
dataType: "json",
success: function (e) {
alert("sucess");
}})
但是我無法將此對象/數組發送到控制器以使用它的數據並遍歷每個行條目。
我的控制器簽名
[HttpPost]
public ActionResutl BllBtn(List<string> dataBll)
{
}
請指導如何將該對象作為列表獲取,以便我可以以最簡單的方式進入。
您遇到了語法錯誤(瀏覽器控制台將告訴您),並且您正在生成無效的JSON。
data: { "dataBll": dataBLL }
我認為應該可以工作。 要么
data: JSON.stringify({ "dataBll": dataBLL })
最壞的情況 也設置
contentType: 'application/json; charset=UTF-8'
作為ajax調用中的另一個選項。
下一個問題是您嘗試將List<string>
dataBll
方法中,但是dataBll
是具有以下屬性的復雜對象:
id, ctype, cpath, ckey, ckey
首先,您不能在同一對象中定義ckey兩次,其次,您的JSON對象與C#方法中的類型不兼容。 您需要定義一個類,例如
public class myNewType
{
public string id {get; set; }
public string ctype {get; set; }
public string cpath {get; set; }
public string ckey {get; set; }
}
然后接受List<myNewType>
作為方法的參數:
public ActionResult BllBtn(List<myNewType> dataBll)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.