使用jquery HybridDictionary作为操作参数发布数据

时间:2016-10-17 09:11:29

标签: c# jquery asp.net-mvc dictionary

我想用这种格式将数据发布到动作控制器

{
  "key1" : "value1",
  "key2" : "value2"
}

我想将这些值映射到我使用的jquery帖子HybridDictionary

$.ajax("/profile/post",
    {
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify({ "key1": "value1", "key2": "value2" }),
        method: "POST",
        async: true,
        success(e) { defer.resolve(e); },
        error(e) { defer.reject(e); }
    });

我的动作控制器我得到了Post(HybridDictionary data)但似乎永远不会被贴出来的价值我做错了什么?

更新

这是我获得这些值的格式

var c= new HybridDictionary();
c.Add("hello", "world");
c.Add("hello2", "world2");
c.Add("hello3", "world3");

1 个答案:

答案 0 :(得分:1)

你的ajax调用没有任何问题。问题是你没有模型绑定器设置来在调用执行时反序列化你的json对象。您可以在 App_Start 文件夹中创建JsonModelBinder课程,此代码为:

public class JsonModelBinder : DefaultModelBinder
{
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (!IsJsonRequest(controllerContext))
            {
                return base.BindModel(controllerContext, bindingContext);
            }

            // Get the JSON data that's been posted
            var request = controllerContext.HttpContext.Request;
            //in some setups there is something that already reads the input stream if content type = 'application/json', so seek to the begining
            request.InputStream.Seek(0, SeekOrigin.Begin);
            var jsonStringData = new StreamReader(request.InputStream).ReadToEnd();

            // Use the built-in serializer to do the work for us
            return new JavaScriptSerializer()
                .Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);

            // -- REQUIRES .NET4
            // If you want to use the .NET4 version of this, change the target framework and uncomment the line below
            // and comment out the above return statement
            //return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);
        }

        private static bool IsJsonRequest(ControllerContext controllerContext)
        {
            var contentType = controllerContext.HttpContext.Request.ContentType;
            return contentType.Contains("application/json");
        }
}

    public static class JavaScriptSerializerExt
    {
        public static object Deserialize(this JavaScriptSerializer serializer, string input, Type objType)
        {
            var deserializerMethod = serializer.GetType().GetMethod("Deserialize", BindingFlags.NonPublic | BindingFlags.Static);

            // internal static method to do the work for us
            //Deserialize(this, input, null, this.RecursionLimit);

            return deserializerMethod.Invoke(serializer,
                new object[] { serializer, input, objType, serializer.RecursionLimit });
        }
    }

然后在 Global.asax.cs Application_Start方法中注册:

ModelBinders.Binders.DefaultBinder = new JsonModelBinder();