In this Article, I describe what is partial view in asp.net mvc. Partial views are used to encapsulate reusable view logic and it simplify the complexity of views. We can use one partial view on multilple views , where we need similar kind of view logics.
If you create a partial view in shared folder then you can access a partial view anywhere in your application but if you create partial view in specific folder or models with in that folder or modules only you can access your partial views.
Partial View creation is similar to normal view creation, but check Create as a partial view checkbox before add a partial view.
Render PartialView
@Html.Partial("Partial viewName",Model)
Example
@Html.Partial("_Product", item)
Here _Product is a Partial view name and item is a name of the model.
_Product View
==================
@model MVCApp.Models.Product
<table>
<tr>
<td>
@Model.Name
</td>
<td>
@Model.Description
</td>
</tr>
</table>
Index View
@model IEnumerable<Gnawazexport.Areas.Admin.Models.ProductImage>
@{
ViewBag.Title = "Index";
}
@foreach (var item in Model)
{
@Html.Partial("_Product", item)
}
Description
In this above demonstration, I used product as a partial view and use this partial in index view. Similarly we can use this partial wherever you need.
Post your comments / questions
Recent Article
- How to add two numbers in Android Studio? | Source Code
- FindViewByID returns null in android studio -SOLVED
- Saving changes is not permitted in SQL SERVER - [SOLVED]
- Restore of database failed. File cannot be restored over the existing. -[SOLVED]
- One or more projects in the solution were not loaded correctly in Visual Studio 2019 | FIXED
- How to find Laptop's Battery Health?
- SOLVED-Related Field got invalid lookup: icontains error in Django
- How to enable Search Feature in Django admin?
Related Article