We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to handle multiple models in a single view with MVC razor?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps.

First, create a new model (common for all models) and refer all other models that are to be used in the same view. Refer to the following code example.

ViewModel.cs

Next, create controller and pass the common model to it. Refer to the following code example.

ListViewController.cs

In View page, refer only to the common model file. Then, you can differentiate the models by using the variables that are created in the common model file.

For example, for first model you can refer to the model as model.ListModel1. Refer to the following code example.

Index.cshtml

Likewise you can differentiate the models in a single view.

The following screenshot illustrates the output.

Figure 1: Multiple models in a single view

Article ID: Published Date: Last Revised Date: Platform: Control:
4963 05/23/2015 05/23/2015 ASP.NET MVC ListView
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.