栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从jQuery / Ajax将Dictionary作为参数传递给ActionResult方法?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从jQuery / Ajax将Dictionary作为参数传递给ActionResult方法?

最后我想通了!谢谢大家的建议!我终于找到了最好的解决方案,就是通过Http
Post传递JSON并使用自定义ModelBinder将JSON转换为Dictionary。我在解决方案中所做的一件事是创建了一个从Dictionary继承的JsonDictionary对象,以便可以将自定义ModelBinder附加到JsonDictionary类型,并且如果以后将Dictionary作为ActionResult参数使用时,将来也不会引起任何冲突。与JSON的目的不同。

这是最终的ActionResult方法:

public ActionResult AddItems([Bind(Include="values")] JsonDictionary values){    // do something}

和jQuery“ $ .post”调用:

$.post("/Controller/AddItems",{    values: Sys.Serialization.JavascriptSerializer.serialize( {     id: 200,     "name": "Chris" }        )},function(data) { },"json");

然后需要注册JsonDictionaryModelBinder,我将其添加到Global.asax.cs中的Application_Start方法中:

protected void Application_Start(){    ModelBinders.Binders.Add(typeof(JsonDictionary), new JsonDictionaryModelBinder());}

最后,这是我创建的JsonDictionaryModelBinder对象和JsonDictionary对象:

public class JsonDictionary : Dictionary<string, object>{    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<JsonDictionary>(json);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385507.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号