开发者

How to choose whether or not use a MVC 3 Editor Template

I have the following editor template

@model DateTime?
@Html.TextBo开发者_C百科x("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })

This cause that every DateTime? is shown as a ToShortDateString() "without the hour" and with the class datePicker.

But now I have the situation that for some DateTime? I need the hour.

The question is, how can I disable the editor template for individual cases? Or is there a work around for this situation.

Thanks


On your model you can use UIHint attribute to say which editor template to use.

So you could create a new editorfor and call it something like DateTimeWithHour (or some better name!) and then in your model it will be something like:

[UIHint("DateTimeWithHour")]
public DateTime SomeDateTime { get; set; }

If you don't specify a UIHint it will default to your already created datetime editor for.


You can use UIHintAttribute to apply different templates on a case by case basis. You could also pass some additional data (ie. format options) in ViewBag, I think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜