在MVC是它可以覆盖一个编辑模板(In MVC is it possible to override

2019-09-20 00:09发布

是否有可能重写编辑模板?

我在我的模型,用于到达/离开日期日期时间字段 - 这些是使用以下EditorTemplate呈现:

@model Nullable<System.DateTime> 

@if ( Model.HasValue ) { 
   @Html.TextBox( "" , String.Format( "{0:dd/MM/yyyy}" , Model.Value ) , new  { @class = "datepicker span2" } ) 
} 
else { 
   @Html.TextBox( "" , String.Format( "{0:dd/MM/yyyy}" , DateTime.Now ) , new { @class = "datepicker span2" } ) 
} 

...这将格式化日期时间字段,例如:01/08/2012

不过,我也想展示,在另一个领域,日期和时间的预订是由例如:

22/07/2012 08:23

我的模型是:

    public DateTime Arrival { get; set; }
    public DateTime Departure { get; set; }
    public DateTime TimeBooked { get; set; }

我想TimeBooked显示时间,以及 - 但编辑模板显然只是显示的日期。

这能覆盖? 或者有没有这样做的另一种方式?

谢谢,

标记

Answer 1:

您可以添加不同的命名编辑器,显示时间也和使用EditorFor的过载,它接受模板名称作为第二个参数,如:

EditorFor(M => m.TimeBooked, “DateWithTimeTemplate”)

MSDN帮助



Answer 2:

您可以通过使用UIHintAttribute属性。 您可以装饰TimeBooked具有这种属性属性,以便将MVC知道使用哪个编辑器。



Answer 3:

是的,你可以做到这一点通过添加hh:mm占位符..

String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now)

更多的例子,看到这里 ..



文章来源: In MVC is it possible to override an editor template