这是我想要做的事情:
我的观点模型:
public class Inputs
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
}
public class UserViewModel { }
public class LearnerSupportViewModel
{
public LearnerSupportViewModel()
{
Inputs = new Inputs();
SearchRecords = new List<UserViewModel>();
UserRecords = new List<UserViewModel>();
}
public Inputs Inputs { get; set; }
public IList<UserViewModel> SearchRecords { get; set; }
public IList<UserViewModel> UserRecords { get; set; }
}
我的ajax:
$('#btnSearch').click(function (evt) {
// Prevent the button from triggering a form submission.
evt.preventDefault();
//var model = JSON.stringify({ model: form }, null);
$.extend(FormSerializer.patterns, {
validate: /^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*(?:\[\])?$/i
});
var model = $('form#supportForm').serializeJSON();
$.ajax({
url: '/Learner/Search',
type: "GET",
data: model,
success: function (data) {
$('#supportForm').html(data);
}
});
return false;
});
发布模型的内容:
"{\"Inputs\":{\"UserName\":\"user\",\"FirstName\":\"first\",\"LastName\":\"last\",\"EmailAddress\":\"email\",\"City\":\"city\",\"State\":\"state\",\"ZipCode\":\"zip\",\"Country\":\"0\"}}"
但是当我看到控制器动作时
public ActionResult Search(LearnerSupportViewModel model)
model.Inputs的所有属性都为null。
我做错了什么?
答案 0 :(得分:0)
ajax的数据属性对我来说不合适。
将数据属性更改为:
data:{ model : model},
同时将类型从GET
更改为POST
,并在您的操作中添加 HttpPost 属性:
[HttpPost]
public ActionResult Search(LearnerSupportViewModel model)
你的ajax代码:
$.ajax({
url: '@Url.Action("Search","Learner")',
type: "POST",
data:
{
model : model
},
success: function (data) {
$('#supportForm').html(data);
}
});