如何使用ShortDate字符串格式与Html.TextBoxFor [英] How to use a ShortDate string format with Html.TextBoxFor
问题描述
使用实体框架与MVC2,我有一系列日期文本框,我想从在很短的日期格式模型显示数据,但我必须使用Html.TextBoxFor为了更新code到工作(使用HTML.Textbox数据永远不会被保存到模型尝试过)。
<%:Html.TextBoxFor(型号=> model.Item.Date,的String.Format({0:D},Model.Item.Date))% >
我试图操纵字符串格式前pression,并增加了元数据映射到实体框架模型类的部分类,但我仍然得到以下填充我在文本框的形式呈现:
01/01/2011 00:00:00
而不是
01/01/2011
<%:Html.EditorFor(型号=> model.Item.Date)%GT;
和您的视图模型:
[数据类型(DataType.Time)
[DisplayFormatAttribute(ApplyFormatInEditMode = TRUE,DataFormatString ={0:D})]
公众的DateTime日期{搞定;组; }
更新:
完整的示例:
型号:
公共类为MyModel
{
公共项目项目{搞定;组; }
}公共类项目
{
[数据类型(DataType.Time)
[DisplayFormatAttribute(ApplyFormatInEditMode = TRUE,DataFormatString ={0:D})]
公众的DateTime日期{搞定;组; }
}
控制器:
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
返回查看(新为MyModel
{
项目=新项目{日期= DateTime.Now}
});
}
}
查看:
<%:Html.EditorFor(型号=> model.Item.Date)%GT;
Using Entity Framework with MVC2, I have a series of date textboxes, that I want to display data from the model in a short date format, but I have to use Html.TextBoxFor in order for the update code to work (Having tried using HTML.Textbox the data never gets saved to the model).
<%: Html.TextBoxFor(model => model.Item.Date, String.Format("{0:d}", Model.Item.Date))%>
I've tried manipulating the string format expression, and have added metadata to a partial class mapped to the Entity Framework model class, however still I get the following populating my textboxes at form render:
01/01/2011 00:00:00
rather than
01/01/2011
<%: Html.EditorFor(model => model.Item.Date) %>
And on your view model:
[DataType(DataType.Time)]
[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime Date { get; set; }
UPDATE:
Full example:
Model:
public class MyModel
{
public Item Item { get; set; }
}
public class Item
{
[DataType(DataType.Time)]
[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime Date { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyModel
{
Item = new Item { Date = DateTime.Now }
});
}
}
View:
<%: Html.EditorFor(model => model.Item.Date) %>
这篇关于如何使用ShortDate字符串格式与Html.TextBoxFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!