user5375600
user5375600

Reputation:

Following correct practise when building view models

I have run into a caveat with regards to my approach to ASP.NET MVC and viewmodels. Essentially what I do is build a viewmodel in controller/action which merges models together and then passes it to the view.

    [HttpGet]
    public ActionResult MyAction1()
    {
     List<StaffModel> staffList = new List<StaffModel>();
     var qryStaff = context.Staff.Select(c => new { c.ID, c.name});
      foreach (var item in qryStaff )
        {
            StaffModel myStaffViewModel = new StaffModel
            {   
                ID = item.ID, Name = item.Name
            };
            staffList.Add(myStaffViewModel );
        }

So I do the above process and also do it with employees, exactly the same and then put it into employeeList. I then create my viewModel as the view.

           EmployeeStaffViewModel viewModel = new EmployeeStaffViewModel 
        {                
            Staff = staffList,
            Employee = employeeList
        };

I then return the view. I have used employee & staff as an example. I actually have more models I add to the viewModel EmployeeStaffViewModel . It's getting quite big all within the controller action. Should I be creating a ViewModel as a class and then instantiating it in my controller so all the linq and foreach goes in the Model. Therefore I can use it in another controller action.

Thank you for any advice. Will be greatly received.

Upvotes: 0

Views: 84

Answers (1)

JamieD77
JamieD77

Reputation: 13949

Lets say you have 3 classes in your ~/Models folder

StaffModel.cs

public class StaffModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public static Func<Staff, StaffModel> Project = item => new StaffModel
    { 
        ID = item.ID,
        Name = item.Name
    };
}

EmployeeModel.cs

public class EmployeeModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public static Func<Employee, EmployeeModel> Project = item => new EmployeeModel
    {
        ID = item.ID,
        Name = item.Name
    };
}

EmployeeStaffViewModel.cs

public class EmployeeStaffViewModel
{
    public EmployeeStaffViewModel()
    {
        Staff = new List<StaffModel>();
        Employee = new List<EmployeeModel>();
    }
    public List<StaffModel> Staff { get; set; }
    public List<EmployeeModel> Employee { get; set; }
}

The StaffModel and EmployeeModel both have a static Func<> that will map your db entity to your models. These Funcs can be used in your linq queries and expressions which you'll see below.

Your controller action is where you will retrieve your entities from your context. You can simplify your code to not have as many lines as you do.

MyController.cs

[HttpGet]
public ActionResult MyAction1()
{
    var model = new EmployeeStaffViewModel();
    model.Staff = context.Staff.Select(StaffModel.Project);  //Select Staff to StaffModel List
    model.Employee = context.Employee.Select(EmployeeModel.Project); //Select Employee to EmployeeModel List
    return View(model);
}

Upvotes: 1

Related Questions