How to apply partial views in ASP.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.