C# MVC1参数绑定
我正在以不变区域性的形式向服务器传递日期,格式如下C# MVC1参数绑定,c#,asp.net-mvc,culture,C#,Asp.net Mvc,Culture,我正在以不变区域性的形式向服务器传递日期,格式如下 'mm/dd/yy' MVC中的参数绑定无法解析此日期,并为参数返回null。这是人为的,因为IIS运行在使用英国文化的机器上(“dd/mm/yy”工作正常) 我想覆盖服务器上所有日期的解析,以使用不变区域性,如 Convert.ChangeType('12/31/11', typeof(DateTime), CultureInfo.InvariantCulture); 即使日期是另一个对象的一部分 public class MyObj {
'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());
这似乎是一些ppl一直想做的事情。我不明白为什么要在服务器上解析当前区域性中的日期等。客户端必须找出服务器的区域性,才能格式化服务器能够解析的日期
感谢您的帮助 是否可以将日期以格式传递给服务器?我认为不管服务器自己的区域设置如何,服务器都会正确解析这些数据。您的问题是您的自定义模型绑定器无法解析某些输入日期,还是您的自定义模型绑定器从未被调用?如果是前者,那么尝试使用用户浏览器的文化可能会有所帮助
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));
}
}
我在这里解决了这个问题,我遗漏的是,在对象中,日期时间是可以为空的
public class MyObj
{
public DateTime? Date { get; set; }
}
因此我的活页夹没有被取走
如果有人感兴趣,我就是这么做的
binderDictionary.add(typeof(DateTime?), new InvariantBinder<DateTime>());
添加(typeof(DateTime?),new InvariantBinder());
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);
}
}
公共类不变量绑定器:IModelBinder
{
公共对象绑定模型(ControllerContext上下文、ModelBindingContext绑定)
{
字符串名称=binding.ModelName;
IDictionary values=binding.ValueProvider;
如果(!values.ContainsKey(name)| | string.IsNullOrEmpty(values[names].AttemptedValue)
返回null;
return(T)Convert.ChangeType(值[name].AttemptedValue、typeof(T)、CultureInfo.Invariant);
}
}
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);
}
}