Overview:
In the development of ASP. NET Web form, the concept of user control is frequently used to reduce duplication of code and facilitates page modularity, which is also introduced in ASP. "Partial View".
1. Create a partial view:
In Solution Manager, locate the shared folder under the Views folder, right-click Add View, select Create as Partial view,
2. Modify the partial view:
1 <p> p elements in the partial view </p>
3, referencing the distribution view in other views:
1 @Html. Partial ("~/views/shared/includepart.cshtml"); 2 <p> p element in original view </p>
Effect:
4, the second method of referencing, the code is as follows:
1 @Html. Action ("Test", "Home") 2 <p> P element in original view </p>
and modify the test action method in the home controller, the code is as follows:
1 public ActionResult Test () 2 {3 return View ("~/views/shared/includepart.cshtml"); 4 }
The effect after the run is consistent with 3.
5, you can pass parameters to the action, with the following code:
1 @Html. Action ("Test", "Home", new {name = "SHARPL", id = 1}) 2 <p> p element in original view </p>
Modify the code in the partial view to:
<p> I am the Division View </p><p> Blog named: @ViewBag the ID of the .name</p><p> blog is: @ViewBag .id</p>
The code for modifying the test action method in the home controller is as follows:
1 public ActionResult Test (string name,int ID) 2 {3 viewbag.id = id;4 viewbag.name = name;5 return View ("~/views/shared/includepart.cshtml"); 6 }
Post-run effects
6, normal view can also be used as a partial view
The code is as follows:
@Html. Partial ("~/views/view1.cshtml")
Where View1 is the normal view as follows:
1 <p> I am the P element in normal view </p>
Effect:
Excerpt from: http://www.cnblogs.com/SharpL/p/4641886.html
How to apply partial views in ASP.