Articles in this section
Category / Section

How to handle multiple models in a single view with ASP.NET MVC ListView?

3 mins read

Handling multiple models in a single view

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 in ASP.NET MVC ListView. 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

    public class ViewModel
    { 
        public IEnumerable<ListTemplate> ListModel1 = ListTemplateModel.setListTempSource();
        public IEnumerable<ListViewSample> ListModel2 = ListViewSampleModel.setViewTempSource();
    }

 

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

ListViewController.cs

        public ActionResult Index()
        {
            ListTemplateModel.clearSource();
            ListViewSampleModel.clearSource();
            ViewModel mymodel = new ViewModel();
            return View(mymodel);
        }

 

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

@model Sample.Models.ViewModel
    @{
        @Html.EJ().ListView("list1").RenderTemplate(true).Width(700).DataSource(Model.ListModel1).ContentTemplate(@<div class="cont-bg">
            <div class="{{>className}}">
            </div>
            <div class="listrightdiv">
                <span class="templatetext">{{>name}}</span> <span class="designationstyle">{{>designation}}</span>
                <div class="aboutstyle">
                    {{>about}}
                </div>
            </div>
        </div>);
    }
    <br />
    <br />
    @{
        @Html.EJ().ListView("list2").RenderTemplate(true).Width(700).DataSource(Model.ListModel2).ContentTemplate(@<div class="cont-bg">
            <div class="{{>className}}">
            </div>
            <div class="listrightdiv">
                <span class="templatetext">{{>name}}</span> <span class="designationstyle">{{>designation}}</span>
                <div class="aboutstyle">
                    {{>about}}
                </div>
            </div>
        </div>);
    }

Likewise you can differentiate the models in a single view.

 

The following screenshot illustrates the output.

Multiple models in a single view

Figure 1: Multiple models in a single view

Conclusion

I hope you enjoyed learning about how to handle multiple models in a single view with MVC razor.

You can refer to our ASP.NET MVC Listview feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Listview example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied