2

There is a class (Model) User.cs:

namespace BookStore
{
    using System;
    using System.Collections.Generic;
    
    public class User
    {
        public int user_id { get; set; }
        public string user_login_name { get; set; }
        public string user_first_name { get; set; }
        public string user_last_name { get; set; }
    }
}

I want to create a ViewModel to then use it in Views. How should I do it? I create ViewModels folder in the project, create a class UserViewModel.cs in it, then what? I just copy and paste the original User.cs content and put limitations? Like this:

namespace BookStore.Models
{
    public class UserViewModel
    {
        [Editable(false)]
        public int user_id { get; private set; }

        public string user_login_name { get; set; }
        public string user_first_name { get; set; }
        public string user_last_name { get; set; }
    }
}

I did so and now my UserController says it can't implicitly convert type BookStore.User to BookStore.UserViewModel:

public class UserController : Controller
    {
        private DBEntities db = new DBEntities();

        public ActionResult UserDetails()
        {
            UserViewModel user = db.Users.FirstOrDefault(u => u.user_login_name==User.Identity.Name);
            if (user == null)
                return RedirectToAction("Create");
            else
                return View(user);
        }

I know it is important in MVC to separate concerns, domain models and view models should be used for different purposes, but why is there no any detailed info on how to create ViewModels correctly?

  • Conversion won't work for sure, as they are technically completely different types. Consider setting up a mapping between them, e.g. with Automapper – Andrei Jun 23 '16 at 11:15
  • It needs to be `User user = db.Users.FirstOrDefault(....);` and then you initialise a new view model and set its properties based on the data model - `UserViewModel model = new UserViewModel() { user_login_name = user.user_login_name, etc };` –  Jun 23 '16 at 11:19
  • But delete those awful property names and use normal conventions - `LoginName`, not `user_login_name`, FirstName, not `user_first_name` etc –  Jun 23 '16 at 11:21
  • Map it back to an instance of the data model (and new one if its a create method, or get the data model from the database based on the ID if your editing. And note that `[Editable(false)]` does nothing and can be removed, and the ID needs a public setter. Also I suggest you read the answers to [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jun 23 '16 at 11:30
  • Consider using [automapper](http://automapper.org/) which means in many cases (if the property names match) that you can do it one line of code. –  Jun 23 '16 at 11:49
  • @SWA I fall in the same issue, Did you find any articals can help ! – Abdulsalam Elsharif Feb 07 '17 at 18:29
  • @SWA As I get to know MVC I share your surprise that it's so much more difficult to work with view models than base model classes. In small applications I just modify my classes and use them rather than create separate view models as it's significantly simpler. – Philip Stratford Sep 26 '17 at 09:00

0 Answers0