Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTime的MVC模型绑定与使用GET或POST不同_C#_Asp.net Mvc_Asp.net Mvc 4_Internationalization_Model Binding - Fatal编程技术网

C# DateTime的MVC模型绑定与使用GET或POST不同

C# DateTime的MVC模型绑定与使用GET或POST不同,c#,asp.net-mvc,asp.net-mvc-4,internationalization,model-binding,C#,Asp.net Mvc,Asp.net Mvc 4,Internationalization,Model Binding,在对某个日期时间模型属性使用“远程”验证属性时,我遇到了以下不需要的行为 服务器端,我的应用程序区域性定义如下: protected void Application_PreRequestHandlerExecute() { if (!(Context.Handler is IRequiresSessionState)){ return; } Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE"); T

在对某个日期时间模型属性使用“远程”验证属性时,我遇到了以下不需要的行为

服务器端,我的应用程序区域性定义如下:

protected void Application_PreRequestHandlerExecute()
{
    if (!(Context.Handler is IRequiresSessionState)){ return; }
    Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-BE");
}
Globalize.culture("nl-BE");
客户端,我的应用程序区域性定义如下:

protected void Application_PreRequestHandlerExecute()
{
    if (!(Context.Handler is IRequiresSessionState)){ return; }
    Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-BE");
}
Globalize.culture("nl-BE");
案例1:

  • 模型属性

    [Remote("IsDateValid", "Home")]
    public DateTime? MyDate { get; set; }
    
    [Remote("IsDateValid", "Home", HttpMethod = "POST")]
    public DateTime? MyDate { get; set; }
    
  • 控制器动作

    public JsonResult IsDateValid(DateTime? MyDate)
    {
        // some validation code here
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    
    [HttpPost]
    public JsonResult IsDateValid(DateTime? MyDate)
    {
        // some validation code here
        return Json(true);
    }
    
  • 调试
    IsDateValid
    方法时,用户界面中输入的日期
    05/10/2013
    (2013年10月5日)被错误地解释为
    10/05/2013
    (2013年5月10日)
案例2:

  • 模型属性

    [Remote("IsDateValid", "Home")]
    public DateTime? MyDate { get; set; }
    
    [Remote("IsDateValid", "Home", HttpMethod = "POST")]
    public DateTime? MyDate { get; set; }
    
  • 控制器动作

    public JsonResult IsDateValid(DateTime? MyDate)
    {
        // some validation code here
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    
    [HttpPost]
    public JsonResult IsDateValid(DateTime? MyDate)
    {
        // some validation code here
        return Json(true);
    }
    
  • 调试
    IsDateValid
    方法时,在UI中输入的日期
    05/10/2013
    (2013年10月5日)被正确地解释为
    05/10/2013
    (2013年10月5日)

我是否缺少一些使“标准”GET远程验证按需工作的配置?

当为GET绑定数据时,使用的是(即“en-US”),而对于POST
Thread.CurrentThread.CurrentCulture则是。背后的原因是GET URL可能由用户共享,因此应该是不变的。然而,POST从不共享,使用服务器的区域性进行绑定是安全的

如果您确信您的应用程序不需要在来自不同国家的人之间共享URL的选项,那么您可以安全地创建自己的URL,即使GET请求也可以强制使用服务器区域设置

以下是Global.asax.cs中的示例:

protected void Application_Start()
{
    /*some code*/

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

/// <summary>
/// Allows to pass date using get using current server's culture instead of invariant culture.
/// </summary>
public class DateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var date = valueProviderResult.AttemptedValue;

        if (String.IsNullOrEmpty(date))
        {
            return null;
        }

        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

        try
        {
            // Parse DateTimeusing current culture.
            return DateTime.Parse(date);
        }
        catch (Exception)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid.", bindingContext.ModelName));
            return null;
        }
    }
}
受保护的无效应用程序\u Start()
{
/*一些代码*/
添加(typeof(DateTime),newdatetimemodelbinder());
添加(typeof(DateTime?),new DateTimeModelBinder());
}
/// 
///允许使用当前服务器的区域性而不是不变的区域性来使用get传递日期。
/// 
公共类DateTimeModelBinder:IModelBinder
{
公共对象绑定模型(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
var valueProviderResult=bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var日期=valueProviderResult.AttemptedValue;
if(String.IsNullOrEmpty(日期))
{
返回null;
}
bindingContext.ModelState.SetModelValue(bindingContext.ModelName,valueProviderResult);
尝试
{
//使用当前区域性分析DateTime。
返回DateTime.Parse(日期);
}
捕获(例外)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,String.Format(“{0}\”无效。”,bindingContext.ModelName));
返回null;
}
}
}

调试用于分析视图中日期的区域性