有人可以指出我在这里做错了什么吗?我遵循标准方法使用带有$ .ajax请求的jQuery从View调用MVC控制器。我还将模型的名称与参数匹配。 这是我的控制器,在调用ajax调用但在参数中获取空值时会受到影响:
[HttpPost, Route("GetUserSettingsTable")]
public ActionResult GetUserSettingsTable(List<UserSettingsTable> userSettings)
{
return PartialView("_UserSettingsTable.cshtml", userSettings);
}
这是我使用的脚本文件:
var selectedUser = $('#Employees_SelectedValue').val();
var selectedCountries = $('#Countries_SelectedValue').val();
var selectedSourceSystems = $('#SourceSystems_SelectedValue').val();
var userSettings = [];
for (var i = 0, max = Math.max(selectedCountries.length, selectedSourceSystems.length); i < max; i++)
{
userSettings.push({
UserName: selectedUser,
UserCountry: selectedCountries[i] || "",
UserSourceSystem: selectedSourceSystems[i] || ""
});
};
//Here userSettings has values like: [{"121", "AU", "ABC"}, {"121", "IN", "PQR"}, {"121", "NZ", "XYZ"}]
$.ajax({
url: '@Url.Action("GetUserSettingsTable", "Admin")',
type: "POST",
data: JSON.stringify(userSettings),
success : function (result)
{
$('#userSettingsTable').html(result);
},
error: function (result)
{
alert(result);
}
});
此外,我的模型定义如下:
public class UserSettingsTable
{
public string UserName { get; set; }
public string UserCountry { get; set; }
public string UserSourceSystem { get; set; }
}
我无法找到不匹配的位置,因为当我调用控制器时,它在参数值中变为null。任何帮助表示赞赏。
答案 0 :(得分:0)
从数据中删除JSON.stringify(),
$.ajax({
url: '@Url.Action("GetUserSettingsTable", "Admin")',
type: "POST",
data: userSettings,
success : function (result)
{
$('#userSettingsTable').html(result);
},
error: function (result)
{
alert(result);
}
});