Passing a DateTime to controller via URL causing error in ASP .NET MVC 3 (culture)

前端 未结 4 1718
轻奢々
轻奢々 2021-02-04 02:45

My application is setted with pt-BR culture (Date is dd-mm-yyyy) in web.config:



        
4条回答
  •  天命终不由人
    2021-02-04 02:59

    Got the same problem using an @Html.Action(..) in a view. For this situation it can be solved by putting the DateTime in a model:

    public class MyModel
    {
      public DateTime Value {get;set;}
    }
    

    and in the view:

    @Html.Action("MyAction", new { myModel })
    

    Note the new { } around the instance of MyModel, this way the DateTime is not converted to a string. This solution only works for Html.Action() and not for Html.ActionLink() or Url.Action() since MVC is doing a myModel.ToString() in the URL.

提交回复
热议问题