用于以HH:mm格式显示时间的MVC 4编辑器模板 [英] MVC 4 Editor Template for Displaying Time in HH:mm Format
问题描述
使用 MVC 4
&我创建了一个编辑器模板,只显示从 DateTime
对象
HH:mm
格式的时间$ b
编辑器模板代码(TimePicker.cshtml)
@model DateTime?
@ {
String modelValue =;
var dateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
var id =;
var cls =;
var style =;
var placeholder =;
if(ViewData [_ id]!= null)
{
id =(string)ViewData [_ id];
}
if(ViewData [_ class]!= null)
{
cls =(string)ViewData [_ class];
}
if(ViewData [_ style]!= null)
{
style =(string)ViewData [_ style];
}
if(ViewData [_ placeholder]!= null)
{
placeholder =(string)ViewData [_ placeholder];
}
如果(Model.HasValue)
{
if(Model.Value!= DateTime.MinValue)
{
modelValue = Model.Value.ToString(HH:mm,dateFormat);
}
}
}
@ Html.TextBox(,modelValue,
new {@class = cls,@id = id,@style = style, @placeholder = placeholder})
在视图中
@ Html.EditorFor(m => m.StartTime,TimePicker,new {_id =StartTime})
pre>
但是,Final HTML Rendered是
< input id =StartTimename =StartTimetype =textvalue =2013-08-05 0:00:00/>
即使我尝试过硬编码调试值,如下所示
@ Html.TextBox(,00:00,
new {@class = cls,@id = id,@style = style,@占位符=占位符})
仍然我相同
2013-08-05 0 :00:00
而不是0:00
码?
编辑:在快速观看
将名称参数
添加到TextBox中会得到结果。但它附加它的名称属性,我不想要。找到这个截图
解决方案阅读后,这个 MVC 3 - Html.EditorFor似乎在$ .ajax调用之后缓存旧值,我意识到绑定之前,HtmlHelper会调用
ModelState
。
不确定检查ModelState的意图/好处是什么,而不是绑定什么值。 >
但是,在设置值之前,我在控制器中添加了此代码。现在它工作正常。
ModelState.Remove(StartTime);
ModelState.Remove(EndTime);
bookingViewModel.StartTime = Convert.ToDateTime(startTime);
bookingViewModel.EndTime = Convert.ToDateTime(endTime);
返回查看(预订,bookingViewModel);
Working with
MVC 4
& I created a Editor template to show only the time inHH:mm
format fromDateTime
objectEditor Template Code (TimePicker.cshtml)
@model DateTime? @{ String modelValue = ""; var dateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat; var id = ""; var cls = ""; var style = ""; var placeholder = ""; if (ViewData["_id"] != null) { id = (string)ViewData["_id"]; } if (ViewData["_class"] != null) { cls = (string)ViewData["_class"]; } if (ViewData["_style"] != null) { style = (string)ViewData["_style"]; } if (ViewData["_placeholder"] != null) { placeholder = (string)ViewData["_placeholder"]; } if (Model.HasValue) { if (Model.Value != DateTime.MinValue) { modelValue = Model.Value.ToString("HH:mm", dateFormat); } } } @Html.TextBox("", modelValue, new { @class = cls, @id = id, @style = style, @placeholder = placeholder })
In View
@Html.EditorFor(m => m.StartTime, "TimePicker", new { _id = "StartTime"})
But the Final HTML Rendered was
<input id="StartTime" name="StartTime" type="text" value="2013-08-05 0:00:00" />
Even I tried hard coding the value for debugging like below
@Html.TextBox("", "00:00", new { @class = cls, @id = id, @style = style, @placeholder = placeholder })
Still I get same
2013-08-05 0:00:00
instead of0:00
What is the problem in the code?
EDIT: In Quick watch
adding name parameter
to TextBox gives a result. But it appends it with name attribute, which i dont want. Find this screenshot解决方案After reading this MVC 3 - Html.EditorFor seems to cache old values after $.ajax call , I realized that before binding, HtmlHelper will look into
ModelState
.Not sure what is the intention/benefit of checking the ModelState, instead of binding what ever the value supplied.
However I added this code in my controller before setting the value. Now it works fine.
ModelState.Remove("StartTime"); ModelState.Remove("EndTime"); bookingViewModel.StartTime = Convert.ToDateTime(startTime); bookingViewModel.EndTime = Convert.ToDateTime(endTime); return View("Booking", bookingViewModel);
这篇关于用于以HH:mm格式显示时间的MVC 4编辑器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!