MVC 3不绑定可空长 [英] MVC 3 doesn't bind nullable long

查看:100
本文介绍了MVC 3不绑定可空长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个测试网站调试我有一个问题,似乎不是我传递JSON数据错误或MVC只是不能绑定可空多头。我当然使用最新的MVC 3发布。

I made a test website to debug an issue I'm having, and it appears that either I'm passing in the JSON data wrong or MVC just can't bind nullable longs. I'm using the latest MVC 3 release, of course.

public class GetDataModel
{
    public string TestString { get; set; }
    public long? TestLong { get; set; }
    public int? TestInt { get; set; }
}

[HttpPost]
public ActionResult GetData(GetDataModel model)
{
    // Do stuff
}

我张贴一个JSON字符串以正确的JSON内容类型:

I'm posting a JSON string with the correct JSON content type:

{ "TestString":"test", "TestLong":12345, "TestInt":123 }

长不绑定,它总是空。它的工作原理,如果我把在引号的价值,但我不应该这样做,我应该?我是否需要有该值的自定义模型绑定?

The long isn't bound, it's always null. It works if I put the value in quotes, but I shouldn't have to do that, should I? Do I need to have a custom model binder for that value?

推荐答案

我的同事想出了这一种解决方法。解决的办法是把输入流,并使用正则表达式用引号括所有的数字变量到的JavaScriptSerializer诱骗正确deserialising多头。这不是一个完美的解决方案,但它需要这个问题的照顾。

My colleague came up with a workaround for this. The solution is to take the input stream and use a Regex to wrap all numeric variables in quotes to trick the JavaScriptSerializer into deserialising the longs properly. It's not a perfect solution, but it takes care of the issue.

这是一个自定义的模型绑定完成。我用<一个href=\"http://stackoverflow.com/questions/4164114/posting-json-data-to-asp-net-mvc/4164421#4164421\">Posting JSON数据到ASP.NET MVC 中作为一个例子。你必须照顾好,不过,如果输入流访问其他地方。

This is done in a custom model binder. I used Posting JSON Data to ASP.NET MVC as an example. You have to take care, though, if the input stream is accessed anywhere else.

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 jsonStringData = new StreamReader(controllerContext.HttpContext.Request.InputStream).ReadToEnd();

        // Wrap numerics
        jsonStringData = Regex.Replace(jsonStringData, @"(?<=:)\s{0,4}(?<num>[\d\.]+)\s{0,4}(?=[,|\]|\}]+)", "\"${num}\"");

        // Use the built-in serializer to do the work for us
        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");
    }
}

然后把这个在全球:

Then put this in the Global:

ModelBinders.Binders.DefaultBinder = new JsonModelBinder();

现在长期被绑定成功。我称这种现象为的JavaScriptSerializer的错误。还要注意的是多头或可空多头阵列坐上开往就好不带引号。

Now the long gets bound successfully. I would call this a bug in the JavaScriptSerializer. Also note that arrays of longs or nullable longs get bound just fine without the quotes.

这篇关于MVC 3不绑定可空长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆