Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At last I figured it out!! Thanks for the suggestions everyone! I finally figured out the best solution is to pass JSON via the Http Post and use a custom ModelBinder to convert the JSON to a Dictionary. One thing I did in my solution is created a JsonDictionary object that inherits from Dictionary so that I can attach the custom ModelBinder to the JsonDictionary type, and it wont cause any conflicts in the future if I use Dictionary as a ActionResult parameter later on for a different purpose than JSON.</p> <p>Here's the final ActionResult method:</p> <pre><code>public ActionResult AddItems([Bind(Include="values")] JsonDictionary values) { // do something } </code></pre> <p>And the jQuery "$.post" call:</p> <pre><code>$.post("/Controller/AddItems", { values: Sys.Serialization.JavaScriptSerializer.serialize( { id: 200, "name": "Chris" } ) }, function(data) { }, "json"); </code></pre> <p>Then the JsonDictionaryModelBinder needs to be registered, I added this to the Application_Start method within the Global.asax.cs:</p> <pre><code>protected void Application_Start() { ModelBinders.Binders.Add(typeof(JsonDictionary), new JsonDictionaryModelBinder()); } </code></pre> <p>And, finally here's the JsonDictionaryModelBinder object and JsonDictionary object I created:</p> <pre><code>public class JsonDictionary : Dictionary&lt;string, object&gt; { public JsonDictionary() { } public void Add(JsonDictionary jsonDictionary) { if (jsonDictionary != null) { foreach (var k in jsonDictionary.Keys) { this.Add(k, jsonDictionary[k]); } } } } public class JsonDictionaryModelBinder : IModelBinder { #region IModelBinder Members public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext.Model == null) { bindingContext.Model = new JsonDictionary(); } var model = bindingContext.Model as JsonDictionary; if (bindingContext.ModelType == typeof(JsonDictionary)) { // Deserialize each form/querystring item specified in the "includeProperties" // parameter that was passed to the "UpdateModel" method call // Check/Add Form Collection this.addRequestValues( model, controllerContext.RequestContext.HttpContext.Request.Form, controllerContext, bindingContext); // Check/Add QueryString Collection this.addRequestValues( model, controllerContext.RequestContext.HttpContext.Request.QueryString, controllerContext, bindingContext); } return model; } #endregion private void addRequestValues(JsonDictionary model, NameValueCollection nameValueCollection, ControllerContext controllerContext, ModelBindingContext bindingContext) { foreach (string key in nameValueCollection.Keys) { if (bindingContext.PropertyFilter(key)) { var jsonText = nameValueCollection[key]; var newModel = deserializeJson(jsonText); // Add the new JSON key/value pairs to the Model model.Add(newModel); } } } private JsonDictionary deserializeJson(string json) { // Must Reference "System.Web.Extensions" in order to use the JavaScriptSerializer var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return serializer.Deserialize&lt;JsonDictionary&gt;(json); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload