【问题标题】:ASP.NET MVC datetime culture issue when passing value back to controller将值传递回控制器时的 ASP.NET MVC 日期时间文化问题
【发布时间】:2023-03-12 07:29:01
【问题描述】:

我如何告诉我的控制器/模型解析日期时间应该期待什么样的文化?

我正在使用一些this post 将 jquery datepicker 实现到我的 mvc 应用程序中。

当我提交“翻译丢失”的日期时,我没有使用美国格式的日期,所以当它被发送到我的控制器时,它就变成了 null。

我有一个用户选择日期的表单:

@using (Html.BeginForm("List", "Meter", FormMethod.Get))
{
    @Html.LabelFor(m => m.StartDate, "From:")
    <div>@Html.EditorFor(m => m.StartDate)</div>

    @Html.LabelFor(m => m.EndDate, "To:")
    <div>@Html.EditorFor(m => m.EndDate)</div>
}

我为此制作了一个编辑模板,以实现 jquery datepicker:

@model DateTime
@Html.TextBox("", Model.ToString("dd-MM-yyyy"), new { @class = "date" }) 

然后我像这样创建日期选择器小部件。

$(document).ready(function () {
    $('.date').datepicker({ dateFormat: "dd-mm-yy" });
});

这一切都很好。

这是问题开始的地方,这是我的控制器:

[HttpGet]
public ActionResult List(DateTime? startDate = null, DateTime? endDate = null)
{
    //This is where startDate and endDate becomes null if the dates dont have the expected formatting.
}

这就是为什么我想以某种方式告诉我的控制器它应该期待什么文化? 我的模型错了吗?我能告诉它使用哪种文化,比如数据注释属性吗?

public class MeterViewModel {
    [Required]
    public DateTime StartDate { get; set; }
    [Required]
    public DateTime EndDate { get; set; }
}

编辑:this link 解释了我的问题以及一个非常好的解决方案。感谢gdoron

【问题讨论】:

  • 对所有请求使用一种格式。 stackoverflow.com/a/28219557/960997
  • @fomaa 我现在使用带有altFieldaltFormat 选项的日期选择器来为隐藏字段提供文化不变版本的日期(如您提到的ISO8601)。然后提交该字段,我觉得这是一个更好的解决方案。

标签: asp.net-mvc-3 validation data-annotations jquery-ui-datepicker


【解决方案1】:

您可以使用 IModelBinder 更改默认模型绑定器以使用用户文化

   public class DateTimeBinder : IModelBinder
   {
       public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
       {
           var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
           var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);

           return date;    
       }
   }

并在 Global.Asax 中写:

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

this excellent blog 上阅读更多内容,了解 Mvc 框架团队为何为所有用户实施默认文化。

【讨论】:

  • 如果日期无效会抛出异常。
  • 我试过了,但它不适用于 GET,例如来自 ActionLinks。
【解决方案2】:

您可以创建一个 Binder 扩展来处理文化格式的日期。

这是我为处理 Decimal 类型的相同问题而编写的示例,希望您能理解

 public class DecimalModelBinder : IModelBinder
 {
   public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
   {
     ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
     ModelState modelState = new ModelState { Value = valueResult };
     object actualValue = null;
     try
     {
       actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture);
     }
     catch (FormatException e)
     {
       modelState.Errors.Add(e);
     }

     bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
     return actualValue;
  }
}

更新

要使用它,只需像这样在 Global.asax 中声明活页夹

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();
  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);

  //HERE you tell the framework how to handle decimal values
  ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());

  DependencyResolver.SetResolver(new ETAutofacDependencyResolver());
}

然后当模型绑定器必须做一些工作时,它会自动知道该做什么。 例如,这是一个包含一些小数类型属性的模型的操作。我什么都不做

[HttpPost]
public ActionResult Edit(int id, MyViewModel viewModel)
{
  if (ModelState.IsValid)
  {
    try
    {
      var model = new MyDomainModelEntity();
      model.DecimalValue = viewModel.DecimalValue;
      repository.Save(model);
      return RedirectToAction("Index");
    }
    catch (RulesException ex)
    {
      ex.CopyTo(ModelState);
    }
    catch
    {
      ModelState.AddModelError("", "My generic error message");
    }
  }
  return View(model);
}

【讨论】:

  • 用一个例子更新了答案。希望对你有更好的帮助
【解决方案3】:

出现此问题是因为您在表单上使用了 GET 方法。 MVC 中的 QueryString 值提供程序始终使用 Invariant/US 日期格式。见:MVC DateTime binding with incorrect date format

有三种解决方案:

  1. 将您的方法更改为 POST。
  2. 正如其他人所说,在提交之前将日期格式更改为 ISO 8601“yyyy-mm-dd”。
  3. 使用自定义活页夹始终将查询字符串日期视为 GB。如果您这样做,您必须确保所有日期都采用该格式:

    public class UKDateTimeModelBinder : IModelBinder
    {
    private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    
    /// <summary>
    /// Fixes date parsing issue when using GET method. Modified from the answer given here:
    /// https://stackoverflow.com/questions/528545/mvc-datetime-binding-with-incorrect-date-format
    /// </summary>
    /// <param name="controllerContext">The controller context.</param>
    /// <param name="bindingContext">The binding context.</param>
    /// <returns>
    /// The converted bound value or null if the raw value is null or empty or cannot be parsed.
    /// </returns>
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var vpr = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    
        if (vpr == null)
        {
            return null;
    
        }
    
        var date = vpr.AttemptedValue;
    
        if (String.IsNullOrEmpty(date))
        {
            return null;
        }
    
        logger.DebugFormat("Parsing bound date '{0}' as UK format.", date);
    
        // Set the ModelState to the first attempted value before we have converted the date. This is to ensure that the ModelState has
        // a value. When we have converted it, we will override it with a full universal date.
        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName));
    
        try
        {
            var realDate = DateTime.Parse(date, System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB"));
    
            // Now set the ModelState value to a full value so that it can always be parsed using InvarianCulture, which is the
            // default for QueryStringValueProvider.
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, new ValueProviderResult(date, realDate.ToString("yyyy-MM-dd hh:mm:ss"), System.Globalization.CultureInfo.GetCultureInfoByIetfLanguageTag("en-GB")));
    
            return realDate;
        }
        catch (Exception)
        {
            logger.ErrorFormat("Error parsing bound date '{0}' as UK format.", date);
    
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid.", bindingContext.ModelName));
            return null;
        }
    }
    }
    

【讨论】:

  • ISO 8601 "yyyy-mm-dd" 日期格式对我有用。我在双语形式上苦苦挣扎,这是一个很好的妥协。谢谢。
【解决方案4】:

提交日期时,您应始终尝试以“yyyy-MM-dd”格式提交。这将使它变得独立于文化。

我通常有一个隐藏字段以这种格式保存日期。使用 jQuery UI 的 datepicker 比较简单。

【讨论】:

  • @Dibbyswift:我在考虑隐藏字段,但不确定这是要走的路,因为我不想要不必要的隐藏字段。但现在有了第二个意见,我可能会朝那个方向发展。
  • 隐藏字段的优点是您可以有一个可见的“显示”字段,允许用户以用户友好的格式提供日期,而隐藏字段仅保留在您需要的格式。
【解决方案5】:

为什么不简单地检查数据的文化并将其转换为这样呢?这种简单的方法让我可以在模型中使用强类型日期,显示操作链接并在所需的语言环境中编辑字段,而不必大惊小怪地将其绑定回强类型日期时间:

public class DateTimeBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        return value.ConvertTo(typeof(DateTime), value.Culture);
    }
}

【讨论】:

    【解决方案6】:

    这对我有用

        <system.web>     
           <globalization enableClientBasedCulture="true" uiCulture="Auto" culture="Auto" />
        </system.web>
    

    【讨论】:

    • 不确定我的记忆是否有用,但如果您使用 post 时,这确实有效,但不是 get。点击其中一个答案中的链接:MVC DateTime binding with wrong date format
    【解决方案7】:

    我有一个基于 @gdoron 帖子的 MVC5 更新解决方案。我会分享它以防其他人正在寻找这个。该类继承自DefaultModelBinder,并对无效日期进行异常处理。它还可以处理空值:

    public class DateTimeModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            object result = null;
    
            var modelName = bindingContext.ModelName;
            var attemptedValue = bindingContext.ValueProvider.GetValue(modelName)?.AttemptedValue;
    
            // in datetime? binding attemptedValue can be Null
            if (attemptedValue != null && !string.IsNullOrWhiteSpace(attemptedValue))
            {
                try
                {
                    var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                    result = DateTime.Parse(value.AttemptedValue, CultureInfo.CurrentCulture);
                }
                catch (FormatException e)
                {
                    bindingContext.ModelState.AddModelError(modelName, e);
                }
            }
    
            return result;
        }
    }
    

    就像Global.Asax 中提到的示例一样

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

    【讨论】:

      猜你喜欢
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多