【发布时间】:2023-03-19 19:54:01
【问题描述】:
我遇到了一个超出我水平的奇怪问题,我试图解决这个问题但没有运气。
我正在开发一个简单的 MVC 应用程序,并且我正在使用 ajax 将数据从视图发送到控制器。由于某种原因,控制器只识别了第一个参数,其余的只是空值。我什至尝试放置固定字符串而不是变量,但它们在控制器中仍然显示为空???
观点:
$.ajax({
type: "POST",
url: "../Home/AddItem",
data: "{ItemModel: 'ttt1', ItemName: 'ttt2'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(JSON.stringify(data));
if (data.Success == "Success") {
alert("Item has been added.");
} else {
alert("We were not able to create the offer");
}
},
error: function (exception) {
console.log(exception);
}
});
在 Home 控制器上,我有以下操作:
[HttpPost]
public JsonResult AddItem(string ItemModel, string ItemName)//ItemName is always null??
{
try
{
_DB.Database.ExecuteSqlCommand(@"INSERT INTO ITEMS(iModel, iName) VALUES ({0}, {1})", ItemModel, ItemName);
return Json(new { Success = "Success" });
}
catch (Exception ex)
{
throw ex;
}
}
【问题讨论】:
标签: javascript c# json asp.net-mvc