日期输入控件和 Html5DateRenderingMode
Date Input Controls and Html5DateRenderingMode
我正处于在 MVC 中学习 Web 应用程序开发的初始阶段,几乎没有与以下代码相关的问题 -
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
<div class="form-group">
@{ Html.Html5DateRenderingMode = Html5DateRenderingMode.Rfc3339; }
@Html.LabelFor(emp => emp.StartDate)
@Html.TextBoxFor(emp => emp.StartDate, new { @class = "form-control" })
@Html.EditorFor(emp => emp.StartDate, new { htmlAttributes = new { @class = "form-control" } })
</div>
呈现为
我知道 TextBoxFor 呈现为 type="text"
,EditorFor 呈现为 type="datetime"
问题 -
1. Why TextBoxFor and EditorFor displays dates differently -
Does Html5DateRenderingMode is only applicable to input type="datetime"?
2. Why controls are rendered differently? I noticed that EditorFor which is rendered as type="datetime" is displayed on full page length input box.
3. When do we use Html5DateRenderingMode in a real-life project?
4. I understand that EditorFor is a preferred approach as it looks at object meta information and decides what control should be rendered.
In what cases, we should prefer TextBoxFor over EditorFor?
谢谢!
- HTML.TextboxFor 将简单地调用 .ToString() 函数
传递对象并显示结果。 HTML.EditorFor 上
另一方面,考虑数据类型,因此支付
注意你的 Html5DateRenderingMode。
- 要了解为什么两个控件呈现不同,您必须检查应用于两者的 css。您可以通过右键单击控件并检查它们来最简单地完成此操作。我猜想,您在 css 中为 input[type=text] 指定了最大宽度,但没有为 input[type=datetime] 指定。
- 我还没用过。如果您坚持使用 EditorFor 并希望将日期时间渲染切换为 Rfc3339,我会将其作为默认值放入您的自定义模板中。
- TextboxFor 是首选解决方案,如果您想为您的 属性 专门获取文本类型的输入。如此处所示:Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor EditorFor can be overwritten to display pretty much anything you want. So, you could write code that displays all your integers as a slider, which might not always be what you want, e.g. for really large numbers that still have to be precise the slider would be hard to work with. Another option would be, if you use libraries which pose a conflict with the elements generated by EditorFor. However, even in that case it would still be possible to stick with EditorFor by using the UIHintAttribute https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute(v=vs.118).aspx
我正处于在 MVC 中学习 Web 应用程序开发的初始阶段,几乎没有与以下代码相关的问题 -
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
<div class="form-group">
@{ Html.Html5DateRenderingMode = Html5DateRenderingMode.Rfc3339; }
@Html.LabelFor(emp => emp.StartDate)
@Html.TextBoxFor(emp => emp.StartDate, new { @class = "form-control" })
@Html.EditorFor(emp => emp.StartDate, new { htmlAttributes = new { @class = "form-control" } })
</div>
呈现为
我知道 TextBoxFor 呈现为 type="text"
,EditorFor 呈现为 type="datetime"
问题 -
1. Why TextBoxFor and EditorFor displays dates differently -
Does Html5DateRenderingMode is only applicable to input type="datetime"?
2. Why controls are rendered differently? I noticed that EditorFor which is rendered as type="datetime" is displayed on full page length input box.
3. When do we use Html5DateRenderingMode in a real-life project?
4. I understand that EditorFor is a preferred approach as it looks at object meta information and decides what control should be rendered.
In what cases, we should prefer TextBoxFor over EditorFor?
谢谢!
- HTML.TextboxFor 将简单地调用 .ToString() 函数 传递对象并显示结果。 HTML.EditorFor 上 另一方面,考虑数据类型,因此支付 注意你的 Html5DateRenderingMode。
- 要了解为什么两个控件呈现不同,您必须检查应用于两者的 css。您可以通过右键单击控件并检查它们来最简单地完成此操作。我猜想,您在 css 中为 input[type=text] 指定了最大宽度,但没有为 input[type=datetime] 指定。
- 我还没用过。如果您坚持使用 EditorFor 并希望将日期时间渲染切换为 Rfc3339,我会将其作为默认值放入您的自定义模板中。
- TextboxFor 是首选解决方案,如果您想为您的 属性 专门获取文本类型的输入。如此处所示:Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor EditorFor can be overwritten to display pretty much anything you want. So, you could write code that displays all your integers as a slider, which might not always be what you want, e.g. for really large numbers that still have to be precise the slider would be hard to work with. Another option would be, if you use libraries which pose a conflict with the elements generated by EditorFor. However, even in that case it would still be possible to stick with EditorFor by using the UIHintAttribute https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute(v=vs.118).aspx