DalSoft / dalsoft-website-comments

0 stars 0 forks source link

blog/index.php/2012/01/10/asp-net-mvc-3-improved-jsonvalueproviderfactory-using-json-net/ #18

Open utterances-bot opened 1 week ago

utterances-bot commented 1 week ago

ASP.NET MVC 3 – Improved JsonValueProviderFactory using Json.Net | DalSoft Ltd

How to create a JsonValueProviderFactory using Json.Net that supports dynamic types.

https://www.dalsoft.co.uk/blog/index.php/2012/01/10/asp-net-mvc-3-improved-jsonvalueproviderfactory-using-json-net/

DalSoft commented 1 week ago

Legacy Comments

Chris Moschini says: January 7, 2013 at 10:08 am This worked for me; to get the entire InputModel passed to the Controller action to be treated as dynamic I had to create a dummy wrapper class:

public class JsonDynamicWrapper
{
   public dynamic o { get; set; }
}

And send all data I want handled as dynamic like:

$.ajax({
…
data: { o: data },
…
});

So basically the o property is typed as dynamic, and the actual data is passed in this property from jquery.

It might also be worth noting I find I get fewer squawks from MVC if I use the following when submitting to Asp.Net MVC:

function postJson(url, data, success, error) {
   return $.ajax({
      url: url,
      data: JSON.stringify(data),
      type: ‘POST’,
      dataType: ‘json’,
      contentType: ‘application/json; charset=utf-8’,
      success: success,
      error: error
   });
}

So the contentType includes an explicit charset to avoid unicode issues from for example form input, and it returns the $.Deferred object the $.ajax() call returns so you can then use .done() and .fail() on the returned object if you like, or the older style success and error callbacks.


Nate Smith says: February 15, 2013 at 5:19 pm Cool. Cheers for posting.


Jon says: March 21, 2013 at 9:11 am This is a really cool solution. I had been grappling with the dynamic property binding issue as well. And, Json.NET is simply a top notch framework, best thing that happened to ASPNET MVC imho.


Pimboden says: February 14, 2014 at 12:25 pm This works for me, but only for Plain-Objects as soon as an Object has a Property that contains another object, this property is set to null Any help?


DalSoft says: February 25, 2014 at 10:03 pm If you take a look at the unit tests in the GitHub repo your see a composite class example.


Nick says: June 25, 2014 at 8:41 am Hi DalSoft – is it possible to make it work with collections?


DalSoft says: June 26, 2014 at 2:56 pm It should already work with collections, post an example if your having problems and I’ll do my best to help.


Ixonal says: August 13, 2014 at 9:02 pm I’m seeing the same sort of problem. I pass in an object containing the two objects I actually want to use as my action parameters, I see the deserialization happen (seemingly correctly), but when the controller action runs, both parameters are null. Is there an intermediate step that’s missing to match up the property names with the parameter names?


DalSoft says: August 14, 2014 at 3:04 pm Hi post the code or a link to a gist and I’ll take a look. It’s working for me for nested classes.


Ixonal says: August 14, 2014 at 6:12 pm Well, let’s see, without getting too verbose, I’m sending a post request containing a serialization of the following object (very much simplified, but _this and geometry are both objects that may contain additional objects and so forth):

{ vm: _this, geometry: geometry }

My controller action has parameters matching these:

public ActionResult RequiresImageRequest(CriteriaViewModel vm, Geometry geometry) {
…
}

But both of these parameters show up as null. I’ve looked in the factory at the converted expando object, and all of the values are there. The only thing I’ve seen that might be the culprit is that, while the outermost object is converted into the DictionaryValueProvider, the inner objects are still ExpandoObjects. Would that cause it not to recognize them correctly?


Ixonal says: August 14, 2014 at 10:10 pm Actually, the reworking in this gist look to fix the issue: https://gist.github.com/DalSoft/1588818


DalSoft says: August 16, 2014 at 10:06 am Thanks for posting back it looks like how you return a ValueProvider has changed after MVC 3. When I get some time I’ll look into it as I’m still sure you can use a ExpandoObject and hardly any code to do this. This is because a ExpandoObject can always be cast to a Dictionary.


Ixonal says: August 19, 2014 at 2:53 pm Yes, the ExpandoObject works fine. I did post some modifications at the end of that gist, as it couldn’t handle lists as the base element before (casting issue).


Rsjump says: November 4, 2014 at 9:55 pm Thanks — works well!


Emmett Hayer says: November 3, 2016 at 12:50 am I really like your writing style, great info , appreciate it for putting up : D.