asp.net mvc - Should my View or ViewModel have the DataType assigned -


which approach has better maintainablility , extendability?

where limitations/restrictions each approach?

put datatype inside viewmodel

or

put datatype/control type in view?

viewmodel

[datatype(datatype.multilinetext)] public string longdescription { get; set; } 

or

view

@html.textareafor(m => m.longdescription) 

as far maintainability concerned, defining in view model best.

given example suppose had in our vm:

[datatype(datatype.multilinetext)] public string longdescription { get; set; } 

and in our view:

@html.editorfor(m => m.longdescription); 

now lets requirements have changed , simple string no longer cuts longdescription, created special customrichtextformat class store it. change vm following:

public customrichtextformat longdescription { get; set; } 

you can create editortemplate called customrichtextformat.cshtml , put in editortemplates folder in view folder, , since used @html.editorfor(m => m.longdescription); in original view, mvc smart enough show custom editor have defined fields of customrichtextformat type.

so summarize, advantage of approach have generic view doesn't require changes despite underlying type of field changing in viewmodel.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -