ASP.NET MVC4 Web API application defines post method to save customer.
Customer is passed in json format in POST request body.
Customer parameter in post method contains null values for properties.
How to fix this so that posted data will passed as customer object ?
If possible Content-Type: application/x-www-form-urlencoded should used since I dont know how to change it in javascript method which posts form.
Controller:
public class CustomersController : ApiController {
public object Post([FromBody] Customer customer)
{
return Request.CreateResponse(HttpStatusCode.OK,
new
{
customer = customer
});
}
}
}
public class Customer
{
public string company_name { get; set; }
public string contact_name { get; set; }
}
Request:
POST http://localhost:52216/api/customers HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
{"contact_name":"sdfsd","company_name":"ssssd"}
Best Answer
EDIT : 31/10/2017
The same code/approach will work for Asp.Net Core 2.0 as well. The major difference is, In asp.net core, both web api controllers and Mvc controllers are merged together to single controller model. So your return type might be
IActionResult
or one of it's implementation (Ex :OkObjectResult
)Use
You need to use
JSON.stringify
method to convert it to JSON string when you send it,And the model binder will bind the json data to your class object.
The below code will work fine (tested)
Result
contentType
property tells the server that we are sending the data in JSON format. Since we sent a JSON data structure,model binding will happen properly.If you inspect the ajax request's headers, you can see that the
Content-Type
value is set asapplication/json
.If you do not specify contentType explicitly, It will use the default content type which is
application/x-www-form-urlencoded;
Edit on Nov 2015 to address other possible issues raised in comments
Posting a complex object
Let's say you have a complex view model class as your web api action method parameter like this
and your web api end point is like
At the time of this writing, ASP.NET MVC 6 is the latest stable version and in MVC6, Both Web api controllers and MVC controllers are inheriting from
Microsoft.AspNet.Mvc.Controller
base class.To send data to the method from client side, the below code should work fine
Model binding works for some properties, but not all ! Why ?
If you do not decorate the web api method parameter with
[FromBody]
attributeAnd send the model(raw javascript object, not in JSON format) without specifying the contentType property value
Model binding will work for the flat properties on the model, not the properties where the type is complex/another type. In our case,
Id
andName
properties will be properly bound to the parameterm
, But theTags
property will be an empty list.The same problem will occur if you are using the short version,
$.post
which will use the default Content-Type when sending the request.