1

Why is Web API model binding so complicated by default: I tried a lot of combinations but none of them seem to work. This is ajax request:

var dataString = JSON.stringify({
            request: Request
        });
var request = Request;
$.ajax({
        type: "POST",
        data: '='+dataString,
        // contentType: 'application/x-www-form-urlencoded',
        contentType: "application/json; charset=utf-8",
        dataType: "json",

This is controller:

public AccResultObject Post(string jezik, string page, string size, string sort,AccRequestViewModel model)

This is AccRequestViewModel

public class AccRequestViewModel
{
    public AccRequestObject request { get; set; }
}

and this is AccRequestObject:

public class AccRequestObject
    {

        public int FM { get; set; }
        public int Budget { get; set; }
        public string WebCatID { get; set; }


        public int Distance { get; set; }
    }

Whatever I do, controller gets null value.

I tried this also. It seems very logical:

 var dataString = JSON.stringify({
            request: Request
         });
 $.ajax({
        type: "POST",
        data: dataString, ...

and controller receives AccRequestObject:

  public AccResultObject Post(string jezik, string page, string size, string sort,[FromBody] AccRequestObjectmodel)

It works great except this small problem. Values are not binded.

3
  • 1
    What is the structure of the "Request" which is being stringified? You also don't need to prepend '=' to the data part of the ajax call. Commented Nov 21, 2014 at 9:03
  • I find out that I need to prepend '=' in WEB API Commented Nov 21, 2014 at 9:06
  • You shouldn't need to and as you're getting nulls in the binder maybe try removing it :-) Please provide the json you are trying to POST as without it it's hard to determine the issue Commented Nov 21, 2014 at 12:02

2 Answers 2

2

Make sure the properties of each object you are creating in javascript match exactly to the code model. If a property name is uppercase in the code model, it needs to be uppercase in the js. Other than that, to post a model with jquery to a Web Api controller is pretty simple:

//code model
public class MyModel{
    public int MyProperty{get;set;}     
}

//js
$.ajax({
        contentType: "application/json",
        dataType: 'json',
        url: 'http://api/to/my/controller',
        data: JSON.stringify({MyProperty: 1}),
        type: "POST"
    });
Sign up to request clarification or add additional context in comments.

2 Comments

can we somehow avoid having the uppercase in the js? Setting the jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); does not work.
@MilenKovachev Id imagine you could write your own: newtonsoft.com/json/help/html/…
1

In the C#, make sure your properties are public properties, not fields.

public class MyBindingModel
{
    public string myProp{ get; set; }
}

1 Comment

They need to be public as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.