[RESOLVED]Displaying partial view in all view pages

I have a partial view that takes a model , i’d like to display this partial view in 80% of my Views.. For example say i have 100 views , i need to display this default partial view in 80 views. How to achieve that. Pls help me with the best solution . Thanks
.. Below is my Partial view 

@model CMS_MVC_DEMO.UI.Models.ViewModel.ContractIDetails


<table class="table table-hover table-condensed">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TYEAR)
        </th>
        <td>
            @Html.DisplayFor(model => model.TYEAR)
        </td>
        <th>
            @Html.DisplayNameFor(model => model.CNO) / @Html.DisplayNameFor(model => model.ContractNAME)
        </th>
        <td>
            @Html.DisplayFor(model => model.CNO) / @Html.DisplayFor(model => model.ContractNAME)
        </td>

    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ContractorNAME)
        </th>
        <td>
            @Html.DisplayFor(model => model.ContractorNAME)
        </td>
         <th>
          
        </th>
        <td>
           
        </td>

    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DistrictNAME)
        </th>
        <td>
            @Html.DisplayFor(model => model.DistrictNAME)
        </td>
         <th>
             @Html.DisplayNameFor(model => model.ContractPeriod)
        </th>
        <td>
             @Html.DisplayFor(model => model.ContractPeriod)
        </td>

    </tr>

</table>

Hi,

     There are two methods to call partial view and passing model from a view :

@Html.RenderPartial("~/Views/Shared/_PartialPageName.cshtml", ModelName); 
And
@Html.Partial("~/Views/Shared/_PartialPageName.cshtml", ModelName);

@Html.RenderPartial :  Result will be written to the Response stream during execution and it returns void. It is faster in execution.
@Html.Partial : Runs as string so you can store value into variable and manipulate on result. 

According to your requirement you can use one of them for all your 80% views.

@Avishek

     But how to pass the model. In order to pass the model we have to make the model ready using any controller action. We cant write the code to prepare the model in all controller. That would be a code redundancy ..  

Hi,

1. If you want to show Partial view in all views, You have to place that partial view in Views/Shared folder.

2. If you want pass model for partial view in each view, you should create property for each class that you are using in view model.

Example:

This is normal view model

Public class Product
{
    Public int Id {get;set;}
    Public string Name {get;set;}
    Public Category Category {get;set;} // here the category is property of Product
}

This is partial view model

Public class Category
{
    Public int Id{get;set;}
    Public string Name{get;set;}    
}

In view:

@model MvcTutorials.Models.Product

@Html.Partial("_Category",Model.Category)

Thanks,

Seshasayana

@Html.RenderPartial()

The above is just to render the Partial view in your page. But if in 80% pages it is going to be used, you can do one thing.

Create a nested layout which will have the 

@{

Layout="PATH TO SHARED LAYOUT";

}

and inside this nested layout have your Partial view rendered.

Then in every page wherever it is required, use in that page

@{

Layout="PATH TO NESTED LAYOUT"

}

I think the second one would be the best solution for you.

Thanks.

1. How are you planning to provide the model to the partial view?

2. Is the Model data static for this partial even it is in multiple views

3. If the model data is common for this this partial in all the views, the have static class having properties to refer in this partial view

4. finally place this [atrial view in shared layout which is your master layout for the views

If all the controllers cannot return ContractIdetails as part of its returned model. I can suggest to use @Html.Action instead of @Html.Partial.

Write an action method in the main controller which will generate the model

public class HomeController : Controller
{
        public ActionResult DisplayContract()
        {
            ContractIDetails objContractIdetails = new ContractIDetails();
            //Fille properties of contractIdetails

            return View(objContractIdetails);
        }
}

In the cshtml page, where ever you want to display the partial view. you can call html.action with actionname and controller name

@Html.Action("DisplayContract","Home")

The difference in @Html.Partial, you are calling the view directly by passing the model. In @Html.Action, you are calling the action method which will get the view and put it in the main view. You must also use caching on the action name for performance
as you will be calling it amny times

Leave a Reply