POST json dictionary

后端 未结 10 890
猫巷女王i
猫巷女王i 2020-11-27 05:52

I\'m trying the following : A model with a dictionary inside send it on the first ajax request then take the result serialize it again and send it back to the controller.

10条回答
  •  借酒劲吻你
    2020-11-27 06:20

    Grab the following NuGet package for System.Json, which includes the new JsonValue type. JsonValue is a flexible new JSON representative type that fully supports C# 4 dynamic, and is also an IEnumerable> in the event you wish to treat a payload as a dictionary/associative array.

    You can pick up System.Json (Beta) with NuGet here. It seems System.Json will be included natively in .NET 4.5, as indicated by the documentation pages here.

    You might also want to read the following article to assist in getting JSON HTTP bodies to properly deserialize into JsonValue objects in your Action method parameters:

    JSON, ASP.NET MVC and JQuery - Working with Untyped JSON made easy

    The two relevant pieces of code from the article above would be the DynamicJsonBinder and DynamicJsonAttribute, replicated here for posterity:

    public class DynamicJsonBinder : IModelBinder  
    {  
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)  
        { 
            if (!controllerContext.HttpContext.Request.ContentType.StartsWith  
                  ("application/json", StringComparison.OrdinalIgnoreCase))  
            {  
                // not JSON request  
                return null;  
            }  
    
            var inpStream = controllerContext.HttpContext.Request.InputStream;  
            inpStream.Seek(0, SeekOrigin.Begin);  
    
            StreamReader reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);  
            string bodyText = reader.ReadToEnd();  
            reader.Close();  
    
    
            if (String.IsNullOrEmpty(bodyText))  
            {  
                // no JSON data  
                return null;  
            }  
    
            return JsonValue.Parse(bodyText);  
        }  
    } 
    
    public class DynamicJsonAttribute : CustomModelBinderAttribute
    {
        public override IModelBinder GetBinder()
        {
            return new DynamicJsonBinder();
        }
    }
    

    A relevant sample use case would be:

    public class HomeController : Controller
    {
        public ActionResult Index (T a)
        {
          return View();
        }
    
        public JsonResult A([DynamicJson] JsonValue value)
        {
          dynamic t = value.AsDynamic();
    
          if (t.Name.IsEmpty())
          {
            t = new // t is dynamic, so I figure just create the structure you need directly
            {
                Name = "myname",
                D = new // Associative array notation (woot!): 
                {
                    a = "a",
                    b = "b",
                    c = "c" 
                }
            };
          }
    
          return Json(t);
        }
    }
    

提交回复
热议问题