MVC 1 参数绑定

发布于 2024-10-31 20:07:11 字数 1053 浏览 3 评论 0原文

我正在以不变区域性将日期传递到我的服务器,格式如下

'mm/dd/yy'

MVC 中的参数绑定无法解析此日期并为参数返回 null。这可能是因为 IIS 运行在使用英语文化的计算机上(“dd/mm/yy”工作正常)。

我想覆盖服务器上所有日期的解析,以像这样使用不变文化...

Convert.ChangeType('12/31/11', typeof(DateTime), CultureInfo.InvariantCulture);

即使日期是另一个对象的一部分...

public class MyObj
{
    public DateTime Date { get; set; }
}

我的控制器方法是这样的...

public ActionResult DoSomethingImportant(MyObj obj)
{
     // use the really important date here
     DoSomethingWithTheDate(obj.Date);
} 

日期发送为像这样的 Json 数据....

myobj.Date = '12/31/11'

我尝试将 IModelBinder 的实现添加到 global.asax 中的 binderDictionary 中,

binderDictionary.Add(typeof(DateTime), new DateTimeModelBinder());

但这不起作用,而且

ModelBinders.Binders.Add(typeof(DateTime), new DataTimeModelBinder());

这似乎有些人一直想要这样做。我不明白为什么你会在服务器上的当前文化中解析日期等。客户端必须找出服务器的文化,以便格式化服务器能够解析的日期......

任何帮助表示赞赏!

I'm passing a date to my server in Invariant Culture, the following format

'mm/dd/yy'

The parameter binding in MVC fails to parse this date and returns null for the parameter. This is persumably because IIS is running on a machine using English culture ('dd/mm/yy' works just fine).

I want to override the parsing of all date on my server to use Invariant Culture like so...

Convert.ChangeType('12/31/11', typeof(DateTime), CultureInfo.InvariantCulture);

even when the date is part of another object...

public class MyObj
{
    public DateTime Date { get; set; }
}

My controller method is something like this....

public ActionResult DoSomethingImportant(MyObj obj)
{
     // use the really important date here
     DoSomethingWithTheDate(obj.Date);
} 

The date is being sent as Json data like so....

myobj.Date = '12/31/11'

I've tried adding an implementation of IModelBinder to the binderDictionary in the global.asax

binderDictionary.Add(typeof(DateTime), new DateTimeModelBinder());

That doesn't work, and neither does

ModelBinders.Binders.Add(typeof(DateTime), new DataTimeModelBinder());

This seems like some ppl would want to do all the time. I can't see why you would parse dates etc. in the current culture on the server. A client would have to find out the culture of the server just to format dates the server will be able to parse.....

Any help appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么这么可爱啊 2024-11-07 20:07:11

我已经解决了这里的问题,我错过的是在对象中,日期时间可以为空,

public class MyObj
{
    public DateTime? Date { get; set; }
}

因此我的活页夹没有被拾取。

如果有人感兴趣,这就是我所做的......

  1. 在 global.asax 添加了以下内容

    binderDictionary.add(typeof(DateTime?), new InvariantBinder());
    
  2. 像这样创建一个不变的绑定器

    公共类 InvariantBinder; : IModelBinder
    {
        公共对象BindModel(ControllerContext上下文,ModelBindingContext绑定)
        {
            字符串名称 = 绑定.ModelName;
    
            IDictionary<字符串,ValueProviderResult>值=绑定.ValueProvider;
    
            if (!values.ContainsKey(name) || string.IsNullOrEmpty(values[names].AttemptedValue)
                返回空值;
    
            return (T)Convert.ChangeType(values[name].AttemptedValue, typeof(T), CultureInfo.Invariant);
        }
    }
    

希望这对其他人有用......

I've solved the problem here, what I had missed was that in the object, the datetime was nullable

public class MyObj
{
    public DateTime? Date { get; set; }
}

Hence my binder wasn't being picked up.

If anyone is interested, this is what I did....

  1. In the global.asax added the following

    binderDictionary.add(typeof(DateTime?), new InvariantBinder<DateTime>());
    
  2. Created an invariant binder like so

    public class InvariantBinder<T> : IModelBinder
    {
        public object BindModel(ControllerContext context, ModelBindingContext binding)
        {
            string name = binding.ModelName;
    
            IDictionary<string, ValueProviderResult> values = binding.ValueProvider;
    
            if (!values.ContainsKey(name) || string.IsNullOrEmpty(values[names].AttemptedValue)
                return null;
    
            return (T)Convert.ChangeType(values[name].AttemptedValue, typeof(T), CultureInfo.Invariant);
        }
    }
    

Hope this comes in handy for someone else.....

む无字情书 2024-11-07 20:07:11

您的问题是您的自定义模型绑定程序无法解析某些输入日期或者您的自定义模型绑定程序永远不会被调用吗?如果是前者,那么尝试使用用户浏览器的文化可能会有所帮助。

public class UserCultureDateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        object value = controllerContext.HttpContext.Request[bindingContext.ModelName];
        if (value == null)
            return null;

        // Request.UserLanguages could have multiple values or even no value.
        string culture = controllerContext.HttpContext.Request.UserLanguages.FirstOrDefault();
        return Convert.ChangeType(value, typeof(DateTime), CultureInfo.GetCultureInfo(culture));
    }
}

...

ModelBinders.Binders.Add(typeof(DateTime?), new UserCultureDateTimeModelBinder());

Is your problem that your custom model binder is unable to parse some of the input dates or that your custom model binder never gets called? If it's the former then trying to use the culture of the user's browser might help.

public class UserCultureDateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        object value = controllerContext.HttpContext.Request[bindingContext.ModelName];
        if (value == null)
            return null;

        // Request.UserLanguages could have multiple values or even no value.
        string culture = controllerContext.HttpContext.Request.UserLanguages.FirstOrDefault();
        return Convert.ChangeType(value, typeof(DateTime), CultureInfo.GetCultureInfo(culture));
    }
}

...

ModelBinders.Binders.Add(typeof(DateTime?), new UserCultureDateTimeModelBinder());
っ左 2024-11-07 20:07:11

是否可以以 ISO 8601 格式将日期传递到服务器?我认为服务器会正​​确解析它,无论其自己的区域设置如何。

Is it possible to pass the date to the server in ISO 8601 format? I think the server would parse that correctly regardless of its own regional settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文