[英]Format Date On Binding (ASP.NET MVC)
In my ASP.net MVC app I have a view that looks like this: 在我的ASP.net MVC应用程序中,我有一个看起来像这样的视图:
...
<label>Due Date</label>
<%=Html.TextBox("due")%>
...
I am using a ModelBinder
to bind the post to my model (the due property is of DateTime
type). 我正在使用ModelBinder
将帖子绑定到我的模型(到期属性是DateTime
类型)。 The problem is when I put "01/01/2009" into the textbox, and the post does not validate (due to other data being input incorrectly). 问题是当我将“01/01/2009”放入文本框时,帖子没有验证(由于其他数据输入不正确)。 The binder repopulates it with the date and time "01/01/2009 00:00:00 ". 活页夹以日期和时间 “01/01/2009 00:00:00 ”重新填充。
Is there any way to tell the binder to format the date correctly (ie ToShortDateString()
)? 有没有办法告诉绑定器正确格式化日期(即ToShortDateString()
)?
I just came across this very simple and elegant solution, available in MVC 2: 我刚刚遇到了这个非常简单而优雅的解决方案,可以在MVC 2中找到:
http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx
Basically if you are using MVC 2.0, use the following in your view. 基本上,如果您使用的是MVC 2.0,请在视图中使用以下内容。
<%=Html.LabelFor(m => m.due) %>
<%=Html.EditorFor(m => m.due)%>
then create a partial view in /Views/Shared/EditorTemplates, called DateTime.ascx 然后在/ Views / Shared / EditorTemplates中创建一个名为DateTime.ascx的局部视图
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%=Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) %>
When the EditorFor<> is called it will find a matching Editor Template. 当调用EditorFor <>时,它将找到匹配的编辑器模板。
Decorate the property in your model with the DataType
attribute, and specify that its a Date
, and not a DateTime
: 使用DataType
属性装饰模型中的属性,并指定它是Date
,而不是DateTime
:
public class Model {
[DataType(DataType.Date)]
public DateTime? Due { get; set; }
}
You do have to use EditorFor
instead of TextBoxFor
in the view as well: 你必须在视图中使用EditorFor
而不是TextBoxFor
:
@Html.EditorFor(m => m.Due)
It's a dirty hack, but it seems to work. 这是一个肮脏的黑客,但它似乎工作。
<%= Html.TextBoxFor(model => model.SomeDate,
new Dictionary<string, object> { { "Value", Model.SomeDate.ToShortDateString() } })%>
You get the model binding, and are able to override the HTML "value" property of the text field with a formatted string. 您获得了模型绑定,并且能够使用格式化字符串覆盖文本字段的HTML“value”属性。
I found this question while searching for the answer myself. 我在寻找答案时发现了这个问题。 The solutions above did not work for me because my DateTime is nullable. 上面的解决方案对我不起作用,因为我的DateTime可以为空。 Here's how I solved it with support for nullable DateTime objects. 以下是我通过支持可为空的DateTime对象来解决它的方法。
<%= Html.TextBox(String.Format("{0:d}", Model.Property)) %>
你为什么不用
<% =Html.TextBox("due", Model.due.ToShortDateString()) %>
First, add this extension for getting property path: 首先,添加此扩展以获取属性路径:
public static class ExpressionParseHelper
{
public static string GetPropertyPath<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> property)
{
Match match = Regex.Match(property.ToString(), @"^[^\.]+\.([^\(\)]+)$");
return match.Groups[1].Value;
}
}
Than add this extension for HtmlHelper: 为HtmlHelper添加此扩展名:
public static MvcHtmlString DateBoxFor<TEntity>(
this HtmlHelper helper,
TEntity model,
Expression<Func<TEntity, DateTime?>> property,
object htmlAttributes)
{
DateTime? date = property.Compile().Invoke(model);
var value = date.HasValue ? date.Value.ToShortDateString() : string.Empty;
var name = ExpressionParseHelper.GetPropertyPath(property);
return helper.TextBox(name, value, htmlAttributes);
}
Also you should add this jQuery code: 你还应该添加这个jQuery代码:
$(function() {
$("input.datebox").datepicker();
});
datepicker is a jQuery plugin. datepicker是一个jQuery插件。
And now you can use it: 现在你可以使用它:
<%= Html.DateBoxFor(Model, (x => x.Entity.SomeDate), new { @class = "datebox" }) %>
In order to get strongly typed access to your model in the code behind of your view you can do this: 为了在视图后面的代码中获得对模型的强类型访问,您可以执行以下操作:
public partial class SomethingView : ViewPage<T>
{
}
Where T is the ViewData type that you want to pass in from your Action. 其中T是您要从Action传入的ViewData类型。
Then in your controller you would have an action : 然后在你的控制器中你会有一个动作:
public ActionResult Something(){
T myObject = new T();
T.Property = DateTime.Today();
Return View("Something", myObject);
}
After that you have nice strongly typed model data in your view so you can do : 之后,您在视图中拥有了很好的强类型模型数据,因此您可以执行以下操作:
<label>My Property</label>
<%=Html.TextBox(ViewData.Model.Property.ToShortDateString())%>
I find the best way to do this is to reset the ModelValue 我发现最好的方法是重置ModelValue
ModelState.SetModelValue("due", new ValueProviderResult(
due.ToShortDateString(),
due.ToShortDateString(),
null));
I guess personally I'd say its best or easiest to do it via a strongly typed page and some defined model class but if you want it to be something that lives in the binder I would do it this way: 我个人认为我会通过强类型页面和一些已定义的模型类来说它是最好或最简单的,但如果你想让它成为粘合剂中的东西,我会这样做:
public class SomeTypeBinder : IModelBinder
{
public object GetValue(ControllerContext controllerContext, string modelName,
Type modelType, ModelStateDictionary modelState)
{
SomeType temp = new SomeType();
//assign values normally
//If an error then add formatted date to ViewState
controllerContext.Controller.ViewData.Add("FormattedDate",
temp.Date.ToShortDateString());
}
}
And then use that in the view when creating the textbox ie : 然后在创建文本框时在视图中使用它,即:
<%= Html.TextBox("FormattedDate") %>
Hope that helps. 希望有所帮助。
试试这个
<%:Html.TextBoxFor(m => m.FromDate, new { @Value = (String.Format("{0:dd/MM/yyyy}", Model.FromDate)) }) %>
MVC4 EF5 View I was trying to preload a field with today's date then pass it to the view for approval. MVC4 EF5 View我试图用今天的日期预加载一个字段然后将其传递给视图进行审批。
ViewModel.SEnd = DateTime.Now //preload todays date
return View(ViewModel) //pass to view
In the view, my first code allowed an edit: 在视图中,我的第一个代码允许编辑:
@Html.EditedFor(item.SEnd) //allow edit
Later I changed it to just display the date, the user cannot change it but the submit triggers the controller savechanges 后来我把它更改为只显示日期,用户无法更改它,但提交会触发控制器保存更改
<td>
@Html.DisplyFor(item.SEnd) //show no edit
</td>
When I changed to DisplayFor I needed to add this to ensure the preloaded value was passed back to the controller. 当我更改为DisplayFor时,我需要添加它以确保将预加载的值传递回控制器。 I also need to add HiddenFor's for every field in the viewmodel. 我还需要为viewmodel中的每个字段添加HiddenFor。
@Html.HiddenFor(model => model.SEnd) //preserve value for passback.
Beginners stuff but it took a while to work this out. 初学者的东西,但它需要一段时间来解决这个问题。
This worked for me: mvc 2 这对我有用:mvc 2
<%: Html.TextBoxFor(m => m.myDate, new { @value = Model.myDate.ToShortDateString()}) %>
Simple and sweet! 简单又甜蜜!
A comment of user82646, thought I'd make it more visible. user82646的评论,以为我会让它更加明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.