- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
将 Entity Framework 与 MVC2 结合使用,我有一系列日期文本框,我想以短日期格式显示模型中的数据,但我必须使用 Html.TextBoxFor 才能使更新代码正常工作(具有尝试使用 HTML.Textbox 数据永远不会保存到模型中)。
<%: Html.TextBoxFor(model => model.Item.Date, String.Format("{0:d}", Model.Item.Date))%>
我已经尝试操纵字符串格式表达式,并将元数据添加到映射到 Entity Framework 模型类的部分类中,但是我仍然在表单渲染时得到以下填充文本框的信息:
01/01/2011 00:00:00
而不是
01/01/2011
最佳答案
<%: Html.EditorFor(model => model.Item.Date) %>
在您的 View 模型上:
[DataType(DataType.Time)]
[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime Date { get; set; }
更新:
完整示例:
型号:
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 }
});
}
}
查看:
<%: Html.EditorFor(model => model.Item.Date) %>
关于c# - 如何在 Html.TextBoxFor 中使用 ShortDate 字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3758573/
我想以 MM/dd/yy 格式在文本字段中显示日期。我将模型类描述为: public class VacancyFormViewModel { public int? ID { get; se
在我的数据库中,我存储了数据类型为 decimal 的字段。我在我的 ASP.NET 应用程序中使用完全相同的 (decimal) 数据类型。 为了显示值,这是在我的 View 中。 @Html.Te
请考虑以下示例: 查看模型 public class FooViewModel { public DateTime Date { get; set; } } 控制者 public class
我有一个 SQL Server 2012,其中我有一个包含两列 TITLE 和 MONTH 的 AWARD 表。 TITLE 是 varchar(256),不能为 NULL。 MONTH 是一个整数,
我在 razor 页面中有这段代码。 @{ var countryCode = Model.CountryCode;} @if (countryCode.Equal
好的,我这里有一个奇怪的。我知道该值已正确传递给该属性,因为它工作正常: @Html.TextBox("Foo", Model.Foo, new { @class = "bar" }) 现在如果我这样
这个问题已经有答案了: How to prevent browser from remembering text field content? [duplicate] (1 个回答) 已关闭 6 年前
我的 MVC 应用程序中有一个奇怪的问题。 当用户从下拉列表中选择一个日期时,它会清除 StartDate和 EndDate领域。 我有以下代码片段: Start date: @Model.S
我有两个助手:Html.TextBox 和 Html.TextBoxFor Html.TextBox(someId, someValue, new { @class = "text-box singl
我在 MVC 表单上有以下 TextBoxFor: @Html.TextBoxFor(m => m.SalesSubTotal, null, new { @class = "form-cont
我想使用 html 助手 (TextBoxFor) 屏蔽电话号码 这是我的代码 Model : [Required(ErrorMessageResourceType =
我正在尝试更改有关是否选中复选框的 TextBoxFor 只读属性。我读过其他帖子,但答案似乎对我不起作用。请任何人指出我在下面的代码中错过了什么。 @Html.La
我有一个要求,如 @Html.TextBoxFor 以逗号分隔格式显示货币,不接受负值和特殊字符。同时,当货币不可用时,我将此 TextBoxFor 的值更改为“不可用”。如何在 View 中使用十进
我正在尝试创建一个集中式登录表单,但使用下面的代码,输入文本左对齐,图标右对齐: @Html.TextBoxFo
我目前正在工作中处理一个网站。到目前为止一切正常,除非我将一个函数移动到一个库以便它可以重用。 该函数是一组两个单选按钮的单击事件。单击"is"按钮时,需要出现一个文本框及其标签。单击“否”按钮时,它
我有一个设置为只读的 TextBoxFor,它当前包含一个人的姓氏。我现在需要做的是添加它,以便此人的后缀(II、III、Sr.、Jr. 等)显示在他们姓氏旁边的文本框中。到目前为止,这就是我的姓氏。
我在@Html.TextBoxFor 中显示一些长文本。文本包含换行符、回车符等,但@Html.TextBoxFor 似乎不尊重这些。有什么我可能遗漏的想法吗? 最佳答案 文本框每个定义单行。 您必须
我正在改进我的元素设计,我想在我的 @html.textboxfor 中添加水印。我能够添加它,但它不适用于 IE 8。因此,我想在我的 @html.textboxfor 中添加一个图像,以便用户知道
我在 MVC View 页面中有这个控件作为 @Html.TextBoxFor(m => m.TemplateGroupName, new { tabindex = "1", placeho
我有一个禁用的输入字段显示来自模型的数据。 @Html.LabelFor(model => model.first_name, "Förnamn", new
我是一名优秀的程序员,十分优秀!