roncansan
roncansan

Reputation: 2380

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

I have the following editor template

@model DateTime?
@Html.TextBox("", (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

Upvotes: 0

Views: 217

Answers (2)

Jakub Konecki
Jakub Konecki

Reputation: 46008

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.

Upvotes: 1

lancscoder
lancscoder

Reputation: 8768

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.

Upvotes: 4

Related Questions