I have read a lot on viewmodels including various posts on SO and I am still not sure how to fully implement them. I am creating an auction site and I have my Home index page. What I want to do is display images (eventually a scrollable image box) that display the items that are the current "best deals". So naturally and from my simple understanding of MVC I have created a partial view that I will use to display these images with @Html.Action and I will use the logic in the controller to calculate and display the items with the best deals.
From what I have read it seems that it is best practice to use viewmodels in this case for the logic and the display of these images since I wont need all of the items fields. I was thinking that my viewmodel would have the same properties of the item like the imageFileName and the itemId so it can still link to the details page and also have a new field that would be dealPercentage or something to that effect. How do I set up and use viewmodels in this case, passing in the items to each viewmodel, storing them since they are separate from my database etc. I am extremely new to this and I am missing a fundamental part to MVC that I would like to learn. Thank you for any and all help.
Here are some of my usage of ViewModels, basically View Model is part of MVVM design pattern which is a good practice when displaying your data model to your View.
1.) You can populate additional properties to the Model of your View which is strongly type by using ViewModel. Example, let's say you want to implement your dropdownlist on your Edit Form, you accomplish that by using strongly type helper like DropDownListFor:
@Html.DropDownListFor(x => x.SelectedItem, new SelectList(Model.Items), "Please select an item." )
Instead of using ViewBag and ViewData like in this example:
@Html.DropDownList("SelectedItems", new SelectList((IEnumerable) ViewData["tempItems"], "Id", "Name"), "Please select an item.")
You can add these properties on your ViewModel without affecting your main Model. Since your Model is tied up on your database context.
The advantage of having a strongly type View that is linked to your ViewModel is that any changes on your ViewModel when coding, will notify you via compile time that you need to change it on all the reference of it on your View as well.
2.) You can prevent unnecessary saving on your table records. This is one of the security feature of your website. For example, in editing your record on your website, you can only specify on your ViewModel what are the properties that can be edited:
Example:
[HttpGet]
public ActionResult EditPage()
{
ViewModel vm = new ViewModel();
return View(vm);
}
[HttpPost]
public ActionResult EditPage(ViewModel model)
{
//Get first the Model that you want to edit
ActualModel AM = dbContext.ActualModel.SingleOrDefault(x => x.ID = model.ID);
//Update the properties that you only specify that needs to be edited
AM.Property1 = model.Property1;
AM.Property2 = model.Property2;
dbContext.ActualModel.Add(AM);
dbContext.Entry(AM).State = System.Data.Entity.EntityState.Modified;
dbContext.SaveChanges();
return View(model);
}
This illustration is also applicable when saving or adding new record.
3.) You can encapsulate different objects or Models in one ViewModel. This is almost the same as my example on number one, but this one is one of the reason I started using ViewModel.
Sometimes, I need to get multiple Models on one View at the same time. One of the scenario I typically encounter this is when I cascade data, for example, when I need to load this Model, and I am required to load different Model instance and its properties, definitely you cannot do that on a normal Model, but rather you need a ViewModel for that.
On top of this, you can also visit this: What is ViewModel in MVC? and this ViewModel Best Practices for additional information regarding ViewModel.