Partial Render:
RenderPartial serves the purpose of rendering a UserControl in an ASP.NET MVC application. The views rendered are called PartialViews. In order to use a partial view in your application add a MVC UserControl to your application. The screenshot below shows which project template to select when adding a user control to your MVC application.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge7l-egIrPJFJE0mcnaBKd4h8HGjSsocd3jNl-OQiuohW3G2yTOcR0-l3JEf2JyT90sFiSYPkV3fCwWF5SUu-E06UDjslmYWGl37PnS0aGYWJDoD69j-vw2WfFIOl80Q6XBsxtPBkJdP0E/s320/Untitled1.jpg)
You can add the MVC View UserControl in your current view folder or in the shared folder. First the view folder will be searched for the specified user control followed by the shared folder.
We have added a simple "ViewUserControl.ascx" to our views folder. Now, let's see how we can load the partial views on the page. Inside the view page you can load the partial view using RenderPartial HTML helper method.
User Control:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2iAXqifVRtdkfr12HSchaHaMcCcGUhnorsE1hnVCNcRgnMsl5RToWQB-ZqmfxZ6un8SMLn487869j0DuXg0n74QFDyPyv4CdG_1QwqNtV6W57pgDwW0clBfSmgjhMAlv9jfOxQd18t9LH/s320/Untitled1.jpg)
Parent Page:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5uHgSuIar1BncMrDk0iUMLhpwb8UejHZY2YVzBJXxG2qKaW1_03aBkIDRKCtjCKTs9f7bKONeAhQGGjReloUU5YWBo09yyLStzhVBksjjXjNbnyb82i4jQl5gh5C8tLgQv4cJAU-Dohj8/s320/Untitled1.jpg)
Now, if you run the page you will see that the partial view is loaded inside the view page as shown in the screenshot below:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEqREQzU9NRaS3HU5ncEieY_X1uceWh4lrM651ekTnnzH-K9nR6zu7OQkUHkjCDUtqMYPJJXCr9ha-ou6QW4s-gPUSPOu9o17dtnDRw-he6BER5R9C2ljobpkErLFw0NDYpKvz9KUVT2Bb/s320/Untitled1.jpg)
Passing Data to Partial View:
In the previous example our partial view was independent which means it was not dependent on any data populated by the view page. In real world applications this might not be the case. We may need to pass additional data to our partial view. The good news is that the partial view has access to the same ViewData dictionary which is used by the view page.
Lets add a class with name Category, which I will pass to the partial view:
namespace MvcPV.Models
{
public class Category
{
public string Name {get;set;}
}
}
1. Untyped ViewPages:
ViewDataDictionery is an untyped collection consisting of a string and an object. The string represents the key, and the object holds the actual data. Like any untyped collection, the object can be of any type, but must be cast to the correct type before it is used. Items are referenced via their key in the ViewData property of the ViewUserControl (in the case of a PartialView). The second is a strongly typed collection, where items are properties of the ViewUserControl.Model property.
No modify the Home Controller for the action 'Index' which is responsible to render the partial view:
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
var categories = new List
{
new Category() {Name = "Beverages"},
new Category() {Name = "Condiments"},
new Category() {Name = "Meat"}
};
ViewData["Categories"] = categories;
return View();
}
Now, the Categories.ascx partial view can easily access the ViewData["Categories"] as shown below:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDoEUmiwHqp8oCTGfOf9ILSZQhfXL1ES7XCYNkVPOCqtq4PhVaG-jEzTgMH8KEAJ8DsDPwPeAkzcQyrJzJm9GEoLbLK5vxPQi5jU11eYm_gZ6VwQiDI0hggZNi9xBd3cTnOTLITCVQ5XTX/s320/Untitled1.jpg)
Now, if you run the page you will see that the partial view is loaded inside the view page as shown in the screenshot below:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5rX1De5OebDchuKRrnpR7j4Ny4izQBLjKAlOObs75Sdh5m7yjRTNwEtdYhMCtTaZp65hE_n7YF9IbeahGYS9MlQ39s51l6v8BS_FmTTe0p7MVBa7598TQT7mxTCNaNcsWpht-XWbRVZ2H/s320/Untitled.jpg)
2. Strongly Typed Views
As of now we show that the ViewData dictionary is shared between the view page and the view user control (partial view). We can even make it better by strong typing the partial view and sending the model as a second parameter to the RenderPartial method. The code below shows how to make the ViewUserControl as a strongly typed view which can handle IEnumerable
Now modify the user control:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglrkvWJD4aPT6EFOn4gk8Yqp33dKpp4oTcp1oWeopojuquEe-a6GEGmi_v3JEAopq1vmaBzvFsyo52PGcHJunx57ekY-iIOJ0Yjtw7Yrj0W-v-oT_K1VXgQCgBGwT-RFcrKaHaoD6DcFo_/s320/Untitled1.jpg)
Also modify the parent page to pass a strong type data to partial view:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAEDvUpzHY9PK9igXvrkhACavoflvCmw5TDrCSUFH5rijHDg6IztLQ9_3BQIt7REU5Nvvhhgtak0TPfI2CYLcshn88E6MYBYk0Tt4H0gPxbUeD3Xb3imK_41XE_cws3kmJBT_Tj7IRWEXk/s320/Untitled1.jpg)
Now Run the application and check how it looks:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWxm4neMLzpU1-nWgV5koMTJOx2e_5LlgPZ6LPPQ4IgslJauoXHmzHW6y4Zm4bVQWWb3I0dCoY9_QzGQjO6KdrqqTRw4_hgYIc3Huyhx_I3CdBp4wHHd2nUzKiX499EYV5vkggQYbBWfAh/s320/Untitled1.jpg)
Since, the ViewData dictionary is shared between the ViewUserControl and the ViewPage you can easily make changes to the object in the ViewUserControl and it will be reflected in the ViewPage using the controller.
No comments:
Post a Comment