blog

Home / DeveloperSection / Blogs / Working with Partial View in MVC Razor

Working with Partial View in MVC Razor

Anonymous User6906 25-Nov-2014

Hi everyone in this blog, I’m explaining about partial view in mvc.

Description:

In general a Partial View is like a web user control in ASP.Net applications. Partial Views and User Controls serve the same purpose.

ASP.NET MVC consists of three main parts - Model, View and Controller. Model represents the business entity or domain object and it manages the state of View. Controller controls the application logic and communication between Model and View. A view represents the user interface, which receives input from user and sends to controller for further action. In this blog, I explain about ASP.NET MVC special views like Partial Views and Layouts.

Uses of Partial Views

  1. A partial view can be used as a reusable component that can be called or used from multiple and different views.
  2. A partial view contains reusable mark-up if you want to render from inside multiple views.
  3. The partial view are used to render a consistent look like header, footer, comments and so on.

Rendering Partial View: We can render a partial view using one of the following 4:

  1. Html.Partial
  2. Html.RenderPartial
  3. Html.Action
  4. Html.RenderAction 

Now in my example I will add a partial view. For this go to Solution Explorer

then select Views -> Shared Folder -> Right-click -> Add View.

Working with Partial View in MVC Razor

 

Give a name to your View and check Create As a partial View as in the

following:

Working with Partial View in MVC Razor

 

Now I am writing a line of text in this Partial View:

Working with Partial View in MVC Razor

 

Now for the Home controller, here I add a new action ShowMyPartialView().

So after it my Home Controller will look such as below:

Working with Partial View in MVC Razor

 

Now for the View -> Home -> Index.cshtml.

Here I am rendering a Partial View using 4 types, so the index.cshtml will be like the following:

 

@{
    ViewBag.Title = "Index";
}
 
<h2>Index</h2>
 
<h3>Rendering Partial View:</h3>
<olclass="round">
    <liclass="one">
        <h5>By Using Html.RenderPartial #</h5>
        @{Html.RenderPartial("MyPartialView"); }
    </li>
 
    <liclass="two">
        <h5>By Using Html.Partial #</h5>
        @Html.Partial("MyPartialView")
    </li>
 
    <liclass="three">
        <h5>By Using Html.RenderAction #</h5>
        @{Html.RenderAction("ShowMyPartialView", "Home");}
    </li>
    <liclass="four">
        <h5>By Using Html.Action #</h5>
        @Html.Action("ShowMyPartialView")
    </li>
</ol>

 

 

Output:

Working with Partial View in MVC Razor

in my next post i'll explain about Online and Offline Events in HTML5


Updated 25-Nov-2014
I am a content writter !

Leave Comment

Comments

Liked By