Pregunta

I googled the problem too many times. But I couldnt find any solution to do this.

I want to create a custom DataType with a default EditorTemplate and DisplayTemplate for use in mvc3 razor.

Model

[DataType("MyCustomDataType")]
public MyType Property { get; set; }
// I mean by MyType any type of data: string, int, datetime and so on

View - Razor

@Html.DisplayFor(m => m.Property)
//or
@Html.EditorFor(m => m.Property)

Actually I want to create a reuseable datatype with default editor-template and display-template.

Do you have any suggestion please? Can you give me a useful link or if you can, a simple example please? Thanks in advance.

¿Fue útil?

Solución

this is an excellent tutorial for custom editor/display template. Although its not in razor syntax, its easily convertible.

If you want to create editor template depending on Datatype attribute, you can get the attribute value using ViewData.ModelMetadata.AdditionalValues["DataTypeAttribute"]

If the value is your custom datatype value, do what ever you want. Otherwise, do the default.

Not sure if it is a good idea. But it should work.

Otros consejos

You can create a new type, i.e. a Person class that you can include in the model that you send to the view. i.e. Model.Person.

By defining a new EditorTemplate and DisplayTemplate with the name Person. By default, calling @Html.EditorTemplateFor(x=>x.Person); will look for a EditorTemplate for the type.

Have a look at the following stackoverflow post on "How to use asp.net mvc Editor Templates"

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top