Thursday, December 15, 2016

Part 17 - What is Partial View in Asp.net MVC




What is Partial View?
        The partial view is the reusable view .It can reduce duplication of view content and allow view elements to be reused.Partial views also have the .cshtml extension
How to call a partial view
@Html.Partial(“PartialViewName”, new { id = 1} );
@{ Html.RenderPartial(“PartialViewName”, viewModel);}
@{ Html.RenderAction(“Method", "Controller”, customViewData);}

Example 


#Partial View - (Partial1.cshtml)

<div>
    <h3>I am in Partial 1</h3>
</div>
  
#Controller code - TestController


using MVCTutorial.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace MVCTutorial.Controllers
{
    public class TestController : Controller
    {

        public ActionResult Index()
        {         

            return View();
        }


        public ActionResult ShowPartial()
        {        

            return PartialView("Partial1");
        }
       
    }
}

 # View Page- (Index.cshtml)


@{
    ViewBag.Title = "Index";
    Layout = null;
}

<div class="container" style="width:40%;margin-top:2%">
    @Html.Partial("Partial1", new { id = 1} );
    @{ Html.RenderPartial("Partial1");}
    @{ Html.RenderAction("ShowPartial");}
</div>

   


All Code Factory

2 comments:

List 5-Luxury said...

Awesome tutorial and create a tutorial for master-detail and master data and insert in the database.

Unknown said...

how did you get the Login partial?