Passing JSON data to a controller method without declaring an object

I use ASP.NET MVC and jQuery to save some data through AJAX calls. I am currently passing some JSON data using the jQuery ajax () function, e.g.

$.ajax({ dataType: 'json', type: 'POST', url: '@Url.Action("UpdateName", "Edit")', data: { id: 16, name: 'Johnny C. Bad' } }); 

using this controller method and helper class.

 public void UpdateName(Poco poco) { var person = PersonController.GetPerson(poco.Id); person.Name = poco.Name; PersonController.UpdatePerson(person); } public class Poco { public int Id { get; set; } public string Name { get; set; } } 

Another way to accept JSON data is to simply use a few arguments like this

 public void UpdateName(int id, string name) { var person = PersonController.GetPerson(id); person.Name = name; PersonController.UpdatePerson(person); } 

This approach is suitable for fewer arguments, but my real world code usually has about 5-10 arguments. Using an object instead of having to declare and use all of these arguments is very convenient.

I wonder if there is another way to accept JSON data as a single object and not declare a class for each controller method in which I want to use this approach. For example, something like this:

 public void UpdateName(dynamic someData) { var person = PersonController.GetPerson(someData.Id); person.Name = someData.Name; PersonController.UpdatePerson(person); } 
+4
source share
4 answers

Possible Solution

Create a specific connecting device, for example

 public class DynamicModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext.HttpContext.Request.Form.AllKeys.Any(x => x == "dynamic")) { dynamic model = new ExpandoObject(); IDictionary underlyingmodel = model; foreach (var key in controllerContext.HttpContext.Request.Form.AllKeys) underlyingmodel.Add(key, (bindingContext.ValueProvider.GetValue(key).RawValue as string[]).First()); return model; } return base.BindModel(controllerContext, bindingContext); } } 

This binder will check for a specific input named "dynamic" and then create a dynamic object

 public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(dynamic input) { ViewBag.Result = input.phrase; return View(); } 
+1
source

You can take the FormCollection form, it will look like this:

 public void UpdateName(FormCollection collection) { var person = PersonController.GetPerson(int.Parse(collection["id"])); person.Name = collection["name"]; person.Age = collection["age"]; PersonController.UpdatePerson(person); } 
+3
source

I'm not quite sure why you want to achieve this, but you can achieve this using dynamic (I have not tried this):

 public void UpdateName(string parameters) { var dynamicObject = Json.Decode(parameters); } 

Json.Decode is in the System.Web.Healpers namespace

And you can pass them in javascript like this:

 var dataObject = JSON.stringify({ id: '1', name: 'John' }); $.ajax({ dataType: 'json', type: 'POST', url: '@Url.Action("UpdateName", "Edit")', data: dataObject }); 
+1
source

I assume that the problem here is to know exactly what common functionality you want to use in the controller between entities. If all this functionality can be unified behind some common property (from the above example, perhaps the Id and Name fields?) Then you can pass IEntity instances back and forth or some base class that contains these 2 properties.

0
source

All Articles