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 use if else statement in c++?
- How to use godaddy domain name with another godaddy hosting account?
- Restore of database 'DATABASE' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
- How to programmatically modifying the AppSetting value in web.config using c#?
- TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
- How to calculate the age from jQuery ui datepicker using c#?
- How to calculate days difference between two dates in c#?
- Changing date format in jQuery ui datepicker
Related Article