Thursday, 16 June 2016

Different Ways to Pass Data to Partial View

Partial views are just Views which you can reuse across your ASP.NET MVC application. If you are from ASP.NET Web Forms background, you can think of Partial View as User Control. Partial views can contain anything – HTML elements for displaying the data or getting the input from the users. In this tip, we are going to see the different ways to pass data to partial view for displaying data to the partial view.

1. Pass Data from Enclosing View to Partial View

@{
   ViewBag.Title = "Index";
   double piValue = 3.14;
}

<h2>Index</h2>
@Html.Partial("_MyPartial", piValue)

In the partial view (_MyPartial.cshtml), I can consume the passed variable value by accessing @Model variable.
Data received is: @Model

2. Pass Data to Partial View using ViewBag/ViewData

 in the below Index action method, I am passing the piValue value from action method to the View.


        public ActionResult Index()
        {
            ViewBag.piValue = 3.14;
            return View();
        }

 In the View(Index.cshtml), I am passing the piValue to the partial view. 


@{
   ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.Partial("_MyPartial", (double) @ViewBag.piValue)

3. Pass Data to Partial View using Strongly Typed Model

 I have created instance of this class and pass it to the View. In the real world, we would be getting the value from database. 

    public ActionResult Index()
      {
            //Assume we are getting below data from the database
        var model = new Employee { Name = "John", Location = "New York" };
        return View(model);
      }

we can just use Modelkeyword to pass the data to the Partial View.


@model PassingData.Models.Employee

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.Partial("_MyPartial",Model)

4. Pass Data to Partial View using ViewData Dictionary

@{
    Html.RenderPartial("_CreateEditDisplay", Model, new ViewDataDictionary { { "Submit", true }, { "Action", "Edit" }, { "ReadOnly", false } });
}

Another Way -

@{
    Html.ViewData.Add(new KeyValuePair&lt;string, object&gt;("Submit", true));
    Html.ViewData.Add(new KeyValuePair&lt;string, object&gt;("Action", "Edit"));
    Html.ViewData.Add(new KeyValuePair&lt;string, object&gt;("ReadOnly", false));

    Html.RenderPartial("_CreateEditDisplay", Model, Html.ViewData);
}

6 comments:


  1. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


    SEO Company in Chennai

    ReplyDelete
  2. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.

    java training in bangalore
    java training in bangalore

    ReplyDelete
  5. Ciitnoida provides Core and java training institute in

    noida
    . We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-

    oriented, java training in noida , class-based build

    of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an

    all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13

    years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best

    Java training in Noida.

    java training institute in noida
    java training in noida

    ReplyDelete
  6. Best Summer Internship In Noida

    These technologies prepare individuals for fields like software programming, technical support, graphic design, software testing, business analytics. Embedded Systems, Industrial Automation Training and more. Candidates go through a series of comprehensive practical sessions where they work on live problems and implement solutions on real-time basis.
    It has a dedicated placement cell which provides 100% placement assistance to students. The benefits that a student gets out of summer training are innumerable. CIITN,Best Summer training Center for B.Tech/CS/CSE/IT/ BCA/MCA/ B.E /M.tech / B.sc/ M.sc/ Engineering Student has already accomplished itself successfully in the field of Training and Development after setting milestones and bringing smiles to the faces of more than 1 Lakh students. CIITN was incorporated in the year 2002 and over the years CIITN has grown remarkably into the greatest training giant of Northern India.
    There is no looking back to technology! Those passionate for it need not worry about the professional prospects it offers. Courses and trainings in the field of Computer Science and Applications open a wide array of choices for individuals. All you need to do it prepare yourself right!

    Summer Internship Course In Noida

    Summer Internship Training In Noida

    ReplyDelete

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)