ridermansb
ridermansb

Reputation: 11069

EditorTemplates > bool - bool.cshtml, DateTime? - DateTime?.cshtml (fail)

Without using "UIHint" how to create .cshtml for certain types of data.

Examples

These are already working!

[DataType(DataType.Url)]
public string Link { get; set; }

will be used

Url.cshtml

[DataType(DataType.MultilineText)]
public string Descrition { get; set; }

will be used

MultilineText.cshtml

These, I am in doubt about how

public IEnumerable<SelectListItem> TypesList { get; set; }

will be used

???????????.cshtml

public DateTime? DateUpdated { get; set; }

will be used

???????????.cshtml

public int? Order { get; set; }

will be used

???????????.cshtml

Questions

In short, do not want to be putting in my ViewModel attributes, like that would work exactly like "Url" and "Multiline"

What should be the file name .cshtml that will be used to int?, IEnumerable<xx> or IEnumerable<string>

Remembering that you can not create files with "<", ">" or "?"

Upvotes: 0

Views: 2262

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039318

By convention (if you don't specify any template name):

  • public IEnumerable<SelectListItem> TypesList { get; set; } will use SelectListItem.cshtml (not that since you have an IEnumerable<T> the editor template will be rendered for each element of the collection and will be called T.cshtml)
  • public DateTime? DateUpdated { get; set; } will use DateTime.cshtml
  • public int? Order { get; set; } will use Int32.cshtml ...

You could also specify a template name in the view:

@Html.EditorFor(x => x.TypesList, "MyTemplate.cshtml")

Assuming TypesList is an IEnumerable<T> then MyTemplate.cshtml will be strongly typed to IEnumerable<T> and not to T.

Upvotes: 4

Related Questions