Should I always use the View Model or should I use ViewData normally?

do you think it is better to use ViewData over the view model?

I have the same exact partial view in several main views. I would like to control how the partial view is rendered, but I would also prefer that the partial view only accept the view model, which is a recordset, just a pure object IEnumerable<>

. I would prefer not to send a full view model object from the main view because it also contains many different properties, objects that control paging, sorting, filtering, etc.

So the question is, should I create a different view model for the partial view, or should I use ViewData ok? I have read that using ViewData

is very bad practice.

With a view of the data, I can just pass the details I want:

@{
    ViewDataDictionary vd = new ViewDataDictionary
    {
        new KeyValuePair<string,object>("WithDelete", Model.WithDelete),
        new KeyValuePair<string,object>("WithRadarCode", Model.WithCode)
    };
}

// ...

@if (Model != null) {
    Html.RenderPartial("_ListOfRecordingsToRender", Model.recordingViewModel, vd);
}

      

At this point, it will be sorted.

My concern is that currently this one *.recordingViewModel

has many different options in my project, due to different models for create / edit, enumerate, create post details, etc. I feel like it might get too messy in my project if I create a model for each activity.

What do you think. Please could you advise on this specific problem. Thanks to

+1


source to share


1 answer


I think you should stick with using ViewModel

, yours ViewModel

is the class that defines your requirements for the view.

My reason is that it will be much more maintainable in the long run. It ViewBag

is a class when used dynamic

, so in your views you have to check if the property exists ViewBag

(and can lead to silly errors like typos) like:

if(ViewBag.PropertyName != null)
{
    // ViewBag.PropertyName is a different property to ViewBag.propertyName
}

      

This type of code can make your browsing look pretty messy. If you are using a strongly typed model you should be able to put most of the logic in your controllers and keep the view as clean as possible, which is a huge plus in my books.

You will also end up at the end (if you use ViewBag

) trying to save it at some point and fight. You're removing one great thing about C #, it's a strongly typed language! ViewBag

is not strongly typed, you might think you are passing in List<T>

, but you can just pass string

.

In the latter case, you will also lose any intellisense functions in Visual Studio.

I feel like it might be too confusing in my project if I create a view model for each activity.

Wouldn't it be useless in your controllers by assigning everything? If it was ViewModel

, you can send it to the Mapping class to map the DTO to your view.



Instead of this:

// ViewModel
var model = new CustomViewModel()
{
    PropertyOne = result.FirstResult,
    PropertyTwo = result.SecondResult,
}
//ViewBag
ViewBag.PropertyOne = result.FirstResult;
ViewBag.PropertyTwo = result.SecondResult;

      

You can do it:

var mapper = new Map();
var model = mapper.MapToViewModel(result);

      

* You will obviously need to provide a mapping class implication, look at something like Automapper

I would also prefer that the partial view only accept the view model, which is a recordset, just a pure IEnumerable <> object. I would rather not send the full view model object from the main view because it also contains many different properties, objects that control paging, sorting, filtering, etc.

That's fine, just create a view model that has a property IEnumerable<T>

. In my opinion, you should try to use strongly typed ViewModel

in all your scripts.

+5


source







All Articles