How ViewModel works in MVC

I really can't figure out how to use the ViewModel in MVC. Let's say I have two simple domain models:

public class Customer
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
}

 public class Order
{
    public int Id { get; set; }
    public string ProductName { get; set; }
}

      

And now my goal would be to create a ViewModel that displays (combines) CustomerName and ProductName to display in the view. I am confused about what to include in the ViewModel to accomplish this. Am I using the same property names as my domain models?

public class MyViewModel
{
    public string CustomerName { get; set; }
    public string ProductName { get; set; }
}

      

How does the ViewModel know that properties come from two different classes? Or am I creating my ViewModel incorrectly?

+3


source to share


4 answers


As I see you have a big design problem.

Suppose you want to display in the user interface only CustomerName

and ProductName

. Then just add these two to your ViewModel class and you're good to go as you described it.

public class MyViewModel
{
    public string CustomerName { get; set; }
    public string ProductName { get; set; }
}

      

Getting data in two variables is not a problem:



Customer customer = service.GetCustomer();
Product product = service.GetProduct()

      

And now that you have everything you need, you can simply set the data and pass it to the view.

MyViewModel viewModel = new MyViewModel();
viewModel.CustomerName = customer.CustomerName;
viewModel.ProductName = product.ProductName;

      

It always depends on what you need to show in the UI and only send what you need and nothing else. You do not need to have exactly the same model that you go elsewhere in your application, Business

, DataAccess

, UI

. You can have something custom if you really need it.

+3


source


You will need to set this yourself in the ViewModel, as a template it might look something like this:

public class MyViewModel
{
    public string CustomerName { get; set; }
    public string ProductName { get; set; }

    public void GetCustomerName(int customerId) 
    {
       CustomerName = CustomerServiceLayer.GetCustomerName(customerId);
       // CustomerService Layer (I.e. a repository that contains this info;
    }

    public void GetProductName(int productId) 
    {
       ProductName = ProductServiceLayer.GetProductName(productId); 
       // ProductService Layer (I.e. a repository that contains this info;
    }

}

      

Then you will have the other two levels of service ( ProductServiceLayer

and CustomerServiceLayer

) that will access the database / repository to get the information you need. This information is then returned to the view (via the ViewModel) and displayed to the user.



Alternatively, you can pass the object Customer

and Product

directly to your ViewModel (via the constructor).

public class MyViewModel 
{
    public Customer MyCustomer { get; set; }
    public Product MyProduct { get; set; }

    public MyViewModel(ICustomer customer, IProduct product) 
    {
       MyCustomer = customer;
       MyProduct = product;
    }
}

      

In this case, you can find all your classes Customer

and Product

introduction.

+2


source


You can do it like this, but you typically create a viewmodel on a render in a get action and then dispatch parts of that view model and process it as a result of the post action. MVC binding does the magic after getting the values ​​posted back from the form.

I would not put the business logic inside the viewmodel, but rather build the viewmodel in your controller using managers / services.

You can also make the viewmodel have complex model types like properties like this.

public class MyViewModel
{
    public Customer Customer { get; set; }
    public Product Product { get; set; }
}

      

+1


source


The ViewModel represents the model that you use to navigate to your view. In your controller, you will receive data and pass it to the ViewModel.

Imagine you have a checkbox in the Golden Client view: it is not good for changing your domain model to add this information, and it is not good practice to mess up your code with Viewbag and Viewdata (imho).

So, you create a model or template that has all the information you need. In our case:

public class MyViewModel
{
    public string CustomerName { get; set; }
    public string ProductName { get; set; }
    public boolean IsGoldCustomer { get; set; }
}

      

Now you will need to convert your Model to a ViewModel and vice versa in order to pass data from the "DOMAIN" to the "VIEW" model.

0


source







All Articles