Kevin Meredith
Kevin Meredith

Reputation: 41909

View to Controller Data Conversion

Working on MVC4 + jQuery software.

On my jQuery front-end/UI, I configured a "Go" button to:

  1. put all related form data into a data object
  2. call var dataToSend = JSON.stringify(data)
  3. send AJAX POST request with dataToSend

For the server-side Controller, I've noticed that there are methods (whose names match controller routes' actions) that contain argument inputs, such as int or string.

How does the client-side JSON gets converted into an int or string for the server-side Controller? Do I need to convert the JSON data in some way?

Upvotes: 0

Views: 224

Answers (1)

James Wilson
James Wilson

Reputation: 289

No, you don't have to do anything special. The MVC model binder will work it's magic and do it's best to map your POST data to Action parameters (it will attempt to map them by name). You don't typically have to worry about doing the type conversion; the model binder will do that for you.

Here's an excellent primer on how the MVC model binder works OotB: http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

Upvotes: 2

Related Questions