CultureInfo的问题Modelbinding两倍asp.net-MVC(2) [英] CultureInfo issue with Modelbinding double in asp.net-mvc(2)

查看:139
本文介绍了CultureInfo的问题Modelbinding两倍asp.net-MVC(2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的jQuery脚本我张贴使用浏览器的的CultureInfo(EN-UK)两个双打了使用为分数分隔符。我的MVC应用程序与区域设置的服务器上运行使用NL-BE的为分数分隔符。

In my Jquery script I post two doubles using the browser's CultureInfo (en-UK) that uses the .as a fraction separator. My MVC app is running on a server with locale nl-BE using the , as a fraction separator.

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult GetGridCell(double longitude, double latitude)
{
    var cell = new GridCellViewModel { X = (int)Math.Round(longitude, 0), Y = (int)Math.Round(latitude, 0) };
    return Json(cell);
}

在modelbinding失败的原因的分析问题。

The modelbinding fails because of the parsing issue.

我认为这将是最好有我的JavaScript设置为en-UK和同样在我的MVC应用程序的modelbinding。但我不知道该怎么办任。
有什么建议?

I think it would be best to have my javascript set to en-UK and the same for the modelbinding in my MVC app. But I don't know how to do either.
Any suggestions?

推荐答案

我不知道有多远的定位去与默认的模型粘合剂(DefaultModelBinder),但你可以很容易地创建一个粘合剂自己能够处理特定文化的解析数据,例如,创建一个新的类,我们把它叫做DoubleModelBinder,copypasta如下:

I'm not sure how far localisation goes with the default model binder (DefaultModelBinder), but you can easily create a binder yourself that can handle the culture specific parsing of the data, e.g, create a new class, let's call it the DoubleModelBinder, copypasta the following:

public class DoubleModelBinder : IModelBinder
{
    /// <summary>
    /// Binds the value to the model.
    /// </summary>
    /// <param name="controllerContext">The current controller context.</param>
    /// <param name="bindingContext">The binding context.</param>
    /// <returns>The new model.</returns>
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var culture = GetUserCulture(controllerContext);

        string value = bindingContext.ValueProvider
                           .GetValue(bindingContext.ModelName)
                           .ConvertTo(typeof(string)) as string;

        double result = 0;
        double.TryParse(value, NumberStyles.Any, culture, out result);

        return result;
    }

    /// <summary>
    /// Gets the culture used for formatting, based on the user's input language.
    /// </summary>
    /// <param name="context">The controller context.</param>
    /// <returns>An instance of <see cref="CultureInfo" />.</returns>
    public CultureInfo GetUserCulture(ControllerContext context)
    {
        var request = context.HttpContext.Request;
        if (request.UserLanguages == null || request.UserLanguages.Length == 0)
            return CultureInfo.CurrentUICulture;

        return new CultureInfo(request.UserLanguages[0]);
    }
}

现在,我们在这里做,是建立我们自己的语言感知双解析器。当我们实现了IModelBinder界面,我们需要创建一个BindModel方法。这就是它的肉做的,但在此之前,我们可以解析任何东西,我们需要得到基于浏览器的语言中的IFormatProvider。因此,我们使用GetUserCulture方法,试图准备浏览器的语言。如果我们不能恢复到当前的文化。

Now, what we are doing here, is establishing our own language-aware double-parser. When we implement the IModelBinder interface, we need to create a BindModel method. This is where the meat of the it is done, but before we can parse anything, we need to get an IFormatProvider based on the browser's language. So, we use the GetUserCulture method to try and ready the browser's language. If we can't revert to the current culture.

当我们有,我们是那么的位置来解析值。我们首先从ValueProvider抓住它(这实在是很多的价值提供者,例如从Form集合,要求回收等的复合),然后我们分析它使用发现的IFormatProvider,这是CultureInfo的我们现在有。

When we have that, we are then in a position to parse the value. We first grab it from the ValueProvider (which is really a composite of many value providers, e.g. from the Form collection, Request collection, etc.), and then we parse it using the discovered IFormatProvider, which is the CultureInfo we now have.

一旦你做到了这一点,这是pretty的小事将其添加到模型绑定集合;

Once you've done that, it's pretty trivial to add it to the model binder collection;

ModelBinder.Binders[typeof(Double)] = new DoubleModelBinder();

试试,看看有没有什么帮助。

Try that and see if that helps.

这篇关于CultureInfo的问题Modelbinding两倍asp.net-MVC(2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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