问题使用jQuery $阿贾克斯()JSON数据传递到.NET MVC行动瓦特/定制BindingModel [英] Problem passing JSON data using jQuery $.ajax() to .NET MVC Action w/ custom BindingModel
问题描述
我想从使用jQuery $阿贾克斯(客户端浏览器的ASP.NET MVC行动传递JSON数据),并使用自定义的模型绑定器将其绑定到.NET类。
I am trying to pass JSON data from the client browser to an ASP.NET MVC Action using jQuery $.ajax() and bind it to a .NET class using a custom ModelBinder.
客户端JavaScript的:
THE CLIENT JAVASCRIPT:
$('#btnPatientSearch').click(function() {
var patientFilter = {
LastName: 'Flinstone',
FirstName: 'Fred'
};
var jsonData = $.toJSON(patientFilter);
$.ajax({
url: '/Services/GetPatientList',
type: 'GET',
cache: false,
data: jsonData,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
timeout: 10000,
error: function() {
alert('Error loading JSON=' + jsonData);
},
success: function(jsonData) {
$("#patientSearchList").fillSelect(jsonData);
}
});
在.NET类JSON数据
THE .NET CLASS FOR THE JSON DATA
[ModelBinder(typeof(JsonModelBinder))]
public class PatientFilter
{
#region Properties
public string IDNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
public DateTime DOB { get; set; }
#endregion
}
MVC的动作。
THE MVC ACTION
public JsonResult GetPatientList(iPatientDoc.Models.PatientFilter patientFilter)
{
自定义ModelBinder的
THE CUSTOM MODELBINDER
public class JsonModelBinder : IModelBinder
{
#region IModelBinder Members
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (bindingContext == null)
throw new ArgumentNullException("bindingContext");
var serializer = new DataContractJsonSerializer(bindingContext.ModelType);
return serializer.ReadObject(controllerContext.HttpContext.Request.InputStream);
#endregion
}
}
自定义ModelBinder的正确调用,然而Request.InputStream是空的,所以没有数据绑定到PatientFilter对象。
The custom ModelBinder is called correctly, yet the Request.InputStream is empty so there is no data to bind to the PatientFilter object.
有什么想法AP preciated。
克里斯
Any thoughts appreciated. Chris
推荐答案
在这几点思考
- 您使用GET请求。我想请求主体始终是空的GET
- 您PatientFilter类没有一个
[DataContract]
属性。我不知道是否会序列化任何 - 我不知道你的
$。阿贾克斯()
电话。我预计该数据选择将只取一个对象而不是一个JSON字符串的。看文档后,我会尝试设置过程数据选项设置为false。
- You use a GET request. I think the request body is always empty for a GET
- Your PatientFilter class doesn't have a
[DataContract]
attribute. I'm not sure if it would serialize anything - I'm not sure about your
$.ajax()
call. I expected that the data option would just take an object instead of a JSON string. After looking at the documentation, I would try to set the processData option to false.
有也为数据选项一个有趣的描述:
There's also an interesting description for the data option:
数据被发送到服务器。它被转换成一个查询字符串,如果尚未字符串。它添加到URL的GET-请求。请参见过程数据选项,以prevent这种自动处理。对象必须是键/值对。如果值是一个数组,jQuery的序列化具有相同的密钥,即多个值{富:BAR1,BAR2]}变成'和;富= BAR1和放大器;富= BAR2
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.
这篇关于问题使用jQuery $阿贾克斯()JSON数据传递到.NET MVC行动瓦特/定制BindingModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!