Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 with ajax not doing ModelBinding in Controller action
    primarykey
    data
    text
    <p>My MVC 3 controller action is not deserializing the JSON data from my AJAX post. </p> <ul> <li>Fiddler shows the data being passed correctly.</li> <li>I put a break point on the var x = "ok!"; line and it hits it every time.</li> <li>When the contentType is removed from the <code>.ajax()</code>, the address object is created but properties are null values.</li> <li>With the contentType in the request, the address object is null.</li> <li>I've tried to put <code>[DataContract]</code> and <code>[DataMembers]</code> on my POCO, no difference</li> <li>I've tried to use an <code>IModelBinder</code>, but the <code>StreamReader(Request.InputStream).ReadToEnd</code> was always "" </li> </ul> <p>Here's the jQuery code:</p> <pre><code> $("#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!'); } }); }); </code></pre> <p>Here's the controller code:</p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult addressValidate(ValidateAddress address) { var x = "ok!"; return new JsonResult() { Data = (x), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } </code></pre> <p>My POCO:</p> <pre><code>public class ValidateAddress { public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } } </code></pre> <p>My Global.asax.cs</p> <pre><code> 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()); } </code></pre> <p>Here's the data from Fiddler:</p> <ul> <li>POST http://localhost.:59872/Settings/addressValidate HTTP/1.1</li> <li>x-requested-with: XMLHttpRequest</li> <li>Accept-Language: en-us</li> <li>Referer: http://localhost.:59872/Settings/Addresses?Length=8</li> <li>Accept: application/json, text/javascript, */*; q=0.01</li> <li>Content-Type: application/json; charset=utf-8</li> <li>Accept-Encoding: gzip, deflate</li> <li>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)</li> <li>Host: localhost.:59872</li> <li>Content-Length: 77</li> <li>Connection: Keep-Alive</li> <li>Pragma: no-cache</li> <li>Cookie: __RequestVerificationToken_Lw__=IBBY7VzoqxMI .... (rest of string snipped)</li> <li>ASP.NET_SessionId=fsjywpn4gqasszgcdgmkqd4p</li> <li></li> <li>{"Address":"1 Main Street","City":"Beach City","State":"FL","Zip":"99999"}</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload