-
-
Save smoak/1100662 to your computer and use it in GitHub Desktop.
ASP.NET MVC 3 JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Assuming you have JSON POSTed to your action in the form: | |
{ "id": 1, "name": "John Smith", "description": "Some description here" } | |
*/ | |
// class for the POSTed data | |
[Serializable] | |
public class SomeJsonModelFormData | |
{ | |
public int id { get; set; } | |
public string name { get; set; } | |
public string description { get; set; } | |
} | |
public class SampleController : Controller | |
{ | |
[HttpPost] | |
public ActionResult SomeAction(SomeJsonModelFormData json) | |
{ | |
// TODO: do stuff with the json here | |
return null; | |
} | |
} | |
// If using MVC 2 | |
// Youll also need to add this line to your Global.asax Application_Start() method: | |
// MVC 3 has this enabled by default so this line can be ignored. | |
protected void Application_Start() | |
{ | |
// register your routes here... | |
// the following line | |
// Enables action methods to send and receive JSON-formatted text and to model-bind the JSON text to parameters of action methods. | |
// See http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonvalueproviderfactory%28v=vs.98%29.aspx | |
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment