我的MVC 3号控制器行动不是淡化我的AJAX哨所的JSON数据。
- Fiddler shows the data being passed correctly.
- I put a break point on the var x = "ok!"; line and it hits it every time.
- When the contentType is removed from the
.ajax()
, the address object is created but properties are null values. - With the contentType in the request, the address object is null.
- I ve tried to put
[DataContract]
and[DataMembers]
on my POCO, no difference - I ve tried to use an
IModelBinder
, but theStreamReader(Request.InputStream).ReadToEnd
was always ""
这里的《家庭法典》:
$("#home-validate-btn").click(function (event) {
var address =
{
Address: $( #Data_HomeAddress ).val(),
City: $( #Data_HomeCity ).val(),
State: $( #Data_HomeState ).val(),
Zip: $( #Data_HomeZip ).val()
};
$.ajax({
url: /Settings/addressValidate ,
type: POST ,
contentType: application/json; charset=utf-8 ,
dataType: json ,
data: $.toJSON(address),
success: function (info) {
alert( ok! );
}
});
});
这里的《控制器法》:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addressValidate(ValidateAddress address)
{
var x = "ok!";
return new JsonResult()
{
Data = (x),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
My POCO:
public class ValidateAddress
{
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
My Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new
{
controller = "Home",
action = "Index"
} // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
}
这里的数据来自菲德尔:
- POST http://localhost.:59872/Settings/addressValidate HTTP/1.1
- x-requested-with: XMLHttpRequest
- Accept-Language: en-us
- Referer: http://localhost.:59872/Settings/Addresses?Length=8
- Accept: application/json, text/javascript, */*; q=0.01
- Content-Type: application/json; charset=utf-8
- Accept-Encoding: gzip, deflate
- User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; .NET4.0C; .NET4.0E; Creative AutoUpdate v1.40.01; msn OptimizedIE8;ENUS)
- Host: localhost.:59872
- Content-Length: 77
- Connection: Keep-Alive
- Pragma: no-cache
- Cookie: __RequestVerificationToken_Lw__=IBBY7VzoqxMI .... (rest of string snipped)
- ASP.NET_SessionId=fsjywpn4gqasszgcdgmkqd4p
- {"Address":"1 Main Street","City":"Beach City","State":"FL","Zip":"99999"}