【问题标题】:Post data using jquery HybridDictionary as action parameter使用 jquery HybridDictionary 作为操作参数发布数据
【发布时间】:2016-10-17 09:11:28
【问题描述】:

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

{
  "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");

【问题讨论】:

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


【解决方案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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多