• How to create Partial View in ASP.NET MVC?

    by  • November 2, 2015 • Microsoft .NET Framework, Microsoft ASP.NET MVC • 0 Comments

    Partial view is special view which renders a portion of view content. It reduces the complexity of code and can be used within multiple views. It is just like regular view with file extension.cshtml. We can use partial view when the situation like we need a header, footer reuse for the ASP.NET MVC web application. We can say that it’s like a user control concept in asp.net.  We cannot add layout view in partial view.

    The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view.

    Here I am going to explain how to create a partial view in ASP.NET MVC.

    Model.cs:

    The Model comes to a simple class containing raw data. The source code of the model class is shown below:

    Controller.cs:

    Since there is no server-side processing, the controller class is also fairly simple and it practically does nothing. it is only passing list to view to show data. The controller class used in the example is shown below:

    _Partial.cshtml:

    Add a partial view to view -> index folder with view name _Partial .The best way to create partial view with name preceded by ‘_’, because the name specifying that it is reusable. The PartialView used in the example is shown below:

    View.cshtml:

    The View used in the example is shown below:




    Namecheap.com
    Avatar of Abdul Rehman

    About

    Abdul Rehman writes on .NET technologies at GetCodeSnippet.com. GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    http://getcodesnippet.com

    Leave a Reply