Elisabeth
Elisabeth

Reputation: 21216

Should my View or ViewModel have the DataType assigned

Which approach has a better maintainablility and extendability?

Where are the limitations/restrictions for each approach?

Put the DataType inside the ViewModel

OR

Put the DataType/Control type in the view?

VIEWMODEL

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

OR

VIEW

@Html.TextAreaFor(m => m.LongDescription)

Upvotes: 2

Views: 800

Answers (3)

Paul Turner
Paul Turner

Reputation: 39675

Generally speaking, it's more powerful to use EditorFor, as this tries to guess what editor you need for a given model element. It's intelligent enough to select custom editor templates where you have defined templates for a given type. For example, if you had a custom editor for images, you could just use the EditorFor syntax and the view engine will resolve the editor for you.

It's a good solution, because it keeps the amount of information repetition down; you only have to specify type information in one place, creating a single source of truth that other components can read from to make decisions. That also means if you make changes to the types of your model, the view will still produce a suitable editor for those types.

However, there are going to be occasions when EditorFor fails to produce the result you need, at which point you will have to get closer to the metal and invoke the appropriate HtmlHelper methods, or even creating extension methods yourself.

You will need to use both approaches; don't be worried about choosing one over the other, as they both fill different needs. Generally, EditorFor will do more for you with less code, but don't by shy about going around it in special cases.

Upvotes: 0

Pete
Pete

Reputation: 3872

As far as maintainability is concerned, defining it in your view model would be best.

Given your example suppose we had this in our VM:

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

and this in our View:

@Html.EditorFor(m => m.LongDescription);

Now lets say that some requirements have changed and a simple string no longer cuts it for your LongDescription, say you created a special CustomRichTextFormat class to store it. You would change your VM to look like the following:

public CustomRichTextFormat LongDescription { get; set; }

You can then create an EditorTemplate called CustomRichTextFormat.cshtml and put it in the EditorTemplates folder in your View folder, and since you used @Html.EditorFor(m => m.LongDescription); in your original View, MVC will be smart enough to show the custom editor you have defined for any fields of the CustomRichTextFormat type.

So to summarize, the advantage of this approach was that you have a truly generic View that doesn't require any changes despite the underlying type of the field changing in the ViewModel.

Upvotes: 1

bluetoft
bluetoft

Reputation: 5443

In my opinion i would suggest having the ViewModel contain the DataType. The reason is because you may then do something like this for your viewModel.

@Html.EditorFor(m => m);

Which will decrease the amount of markup you need in your view.

Upvotes: 0

Related Questions