In this article we learn what a Partial View is and how to call a Partial View and in which scenario which method is good for the call.
What a Partial View is
A Partial View is similar to a User Control in ASP.NET.
Let's create a view for showing some records. Please read my previous article if you don't know How to Call Layout View at Run Time in MVC.
After successful creation of the project let's do some changes to our project.
What a Partial View is
A Partial View is similar to a User Control in ASP.NET.
Let's create a view for showing some records. Please read my previous article if you don't know How to Call Layout View at Run Time in MVC.
After successful creation of the project let's do some changes to our project.
- Right-click on the model folder then select "Add" → "Class" and name it "ListEmployee.cs" and place the following code into it.
- namespace mvcDemo.Models
- {
- public class ListEmployee
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- }
- }
- Go to HomeController and write the following:
- public ActionResult List()
- {
- List<ListEmployee> listEmployee = new List<ListEmployee>();
- listEmployee.Add(new ListEmployee() { Id = 1, Name = "Pramod", Email = "pramod@yahoo.com" });
- listEmployee.Add(new ListEmployee() { Id = 2, Name = "Ravi", Email = "ravi@yahoo.com" });
- listEmployee.Add(new ListEmployee() { Id = 3, Name = "Rahul", Email = "rahul@yahoo.com" });
- listEmployee.Add(new ListEmployee() { Id = 4, Name = "Deepak", Email = "deepak@yahoo.com" });
- return View(listEmployee);
- }
- Right-click on List() and hit the add view option and name it List.
This will write some auto generated code in List.cshtml. - Let's make some changes in List.cshtml.
- @model IEnumerable<mvcDemo.Models.ListEmployee>
- @{
- ViewBag.Title = "List";
- }
- <h2>Employee List</h2>
- <table>
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Id)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Email)
- </th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.Id)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Email)
- </td>
- </tr>
- }
- </table>
Let's make it using a Partial View.
- Expand the Views folder then select Shared folder.
- Right-click on the Shared folder then select Add View and name it _Employee.
- Let's make some changes in _Employee.cshtml.
- @model IEnumerable<mvcDemo.Models.ListEmployee>
- <table>
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Id)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Email)
- </th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.Id)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Email)
- </td>
- </tr>
- }
- </table>
- In List.cshtml delete all the code and paste in the following code.
- @model IEnumerable<mvcDemo.Models.ListEmployee>
- @{
- ViewBag.Title = "List";
- }
- <h2>Employee List</h2>
- @Html.Partial("_Employee")
- Run the project and see the output. We will get what we expected.
Difference between Html.Partial and Html.RenderPartial
- The RenderPartial method return type is void whereas the Partial method returns MvcHtmlString.
- Syntax.
- Calling Html.Partial method
- @Html.Partial(“_partialViewName”);
- Calling Html.RenderPartial method
- @{Html.RenderPartial(“_partialViewName”);}
Note: From the performance perspective rendering directly to the output steam is always better so Html.RenderPartial is a good choice if we don't need any manipulation over output. If manipulation is required over the output stream then Html.Partial is better than Html.RenderPartial.
Source Collected from
http://www.c-sharpcorner.com/UploadFile/b182bf/what-is-partial-view-and-difference-between-html-partial-and/
No comments :
Post a Comment