How to bind model data to partial view

C Sharp – How to bind model data to partial view

We created StudentList view in the view section, which display all the student information as shown below.

We created StudentList view in the view section, which display all the student information as shown below.

Now, instead of creating view for student list we can create the partial view for it, so that it can be useful in many other views just to display student’s information. For example, we can use same partial view, which list all the students of particular standard or students of same age etc. We can create partial view for the following parts:

How to bind model data to partial view

To create a partial view, right click on Shared folder -> select Add -> click on View…

How to bind model data to partial view

Note : If partial view will be shared with multiple views then create it in Shared folder otherwise you can create the partial view in the same folder where it is going to be used.

In the Add View dialogue, enter View name. Select scaffolding template List because we are going to create view to display multiple students’ information. Select Student model class and also check “Create as a partial view” checkbox and click Add.

How to bind model data to partial view

This will create _StudentList.cshtml as shown below.

How to bind model data to partial view

We have removed following action link in auto generated code as we don’t want to display “Create New” link in every view.

How to bind model data to partial view

Now, we can render _StudentList partial view by using Html.Partial(), Html.RenderPartial or Html.RenderAction method.

Let’s use this _StudentList partial view in Index view.

Index action method in Studentcontroller passes IList<student> as shown below.

How to bind model data to partial view

Partial():

The following Index.cshtml view renders _StudentList partial view using Html.Partial() method.

How to bind model data to partial view

As you can see in the above code, First parameter of Partial method specifies partial view name and second parameter is model object.

RenderPartial():

The following Index.cshtml view render _StudentList partial view using Html.RenderPartial() method.

How to bind model data to partial view

RenderAction():

RenderAction method invokes the specified child action method and renders the result in the parent view.

To render partial view using RenderAction method, you need to create action method in StudentController class. So create StudentList action method as shown below.

How to bind model data to partial view

StudentList action method is marked with ChildActionOnly attribute, so that it will be invoke as child action from view, not directly from the URL. Also, notice that StudentList action method injects IList so we don’t need to inject model in Index method.

The following Index.cshtml uses RenderAction method to render _StudentList partial view.

How to bind model data to partial view

Thus, you can render partial view by multiple ways.