MVC分部檢視的使用:Html.Partial/RenderPartial,Html.Action/RenderAction,RenderPage
阿新 • • 發佈:2019-02-08
ASP.NET MVC 裡的部分檢視,相當於 Web Form 裡的 User Control。我們的頁面往往會有許多重用的地方,可以進行封裝重用。
使用部分檢視有以下優點: 1. 可以簡寫程式碼。 2. 頁面程式碼更加清晰、更好維護。
在視圖裡有多種方法可以 載入部分檢視,包括: Partial() 、RenderPartial() 、 Action() 、RenderAction() 、 RenderPage() 方法
一、Partial與RenderPartial
1.Razor 語法: @Html.Partial() 與 @{Html.RenderPartial();}2.區別:Partial 可以直接輸出內容,它內部是 將 html 內容轉換為 string 字元(MVCHtmlString)(進行Html編碼),然後快取起來,最後在一次性輸出到頁面。顯然,這個轉換的過程,會降低效率,所以通常使用 RenderPartial 代替。 這兩者都只是抓取分部檢視頁面類容,不能執行分部檢視方法,所以用Partial或RenderPartial方法來顯示分部檢視不用建立對應的Action,因為不走Action.
3.例項:
普通呼叫分部檢視
主頁 Index.cshtml:
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <h3>我是首頁</h3>
- <section>
- <h2>分部檢視</h2>
- @Html.Partial("~/Views/Templates/Partial1.cshtml")
- //@{Html.RenderPartial("~/Views/Templates/Partial1.cshtml");}
- </section> </div></body></html>
- 分部檢視Partial1.cshtml:
- <table border="1px solid" cellpadding="0" cellspacing="0">
- <tr>
- <th>姓名</th>
- <th>性別</th>
- <th>年齡</th>
- <th>電話</th>
- </tr>
- <tr>
- <td>longxi1</td>
- <td>男</td>
- <td>22</td>
- <td>13521187063</td>
- </tr>
- <tr>
- <td>longxi1</td>
- <td>男</td>
- <td>22</td>
- <td>13521187063</td>
- </tr>
- </table>
- 強型別分部檢視:
- 主頁 Index.cshtml:
- @using WebApplication1.Models
- @{
- Layout = null;
- }
- @{
- List<Student> students = new List<Student>() {
- new Student("zhulongxi",22,"男","13521187063"),
- new Student("zhulongxi",22,"男","13521187063"),
- new Student("zhulongxi",22,"男","13521187063"),
- new Student("zhulongxi",22,"男","13521187063"),
- new Student("zhulongxi",22,"男","13521187063")
- };
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <h3>我是首頁</h3>
- <section>
- <h4>分部檢視</h4>
- @Html.Partial("~/Views/Templates/Partial1.cshtml", students)//如果Partial1.cshtml與Index.cshtml在相同目錄,則可以直接寫成
- @Html.Partial("~/Views/Templates/Partial1.cshtml", students)
- </section> </div></body></html>
分部檢視Partial1.cshtml:
- @using WebApplication1.Models;
- @{
- var studentsList = Model as List<Student>;
- }
- <table border="1px solid" cellpadding="0" cellspacing="0">
- @foreach (Student student in studentsList)
- {
- <tr>
- <th>@student.Name</th>
- <th>@student.Gender</th>
- <th>@student.Age</th>
- <th>@student.Phone</th>
- </tr>
- }
- </table>
1.Razor 語法:@Html.Action()與@{Html.RenderAction();}
2.區別:Action 也是直接輸出,和 Partial 一樣,也存在一個轉換的過程。不如 RenderAction 直接輸出到當前 HttpContext 的效率高。
除此之外,Action與Partial相比,Action訪問了控制器中的Action,執行了Action內部的業務。
3.例項:
Index.cshtml:
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <h3>我是首頁</h3>
- <section>
- <h4>分部檢視</h4>
- @Html.Action("MyPartial", "Home",new { title="學生列表"})
- </section>
- </div>
- </body>
- </html>
HomController:
- public class HomeController : Controller
- {
- // GET: Home
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult MyPartial(string title)
- {
- List<Student> students = new List<Student>() {
- new Student("zhulongxi2",22,"男","13521187063"),
- new Student("zhulongxi2",22,"男","13521187063"),
- new Student("zhulongxi2",22,"男","13521187063"),
- new Student("zhulongxi2",22,"男","13521187063"),
- new Student("zhulongxi2",22,"男","13521187063")
- };
- ViewBag.Data = title;
- return PartialView("~/Views/Templates/Partial2.cshtml",students);
- }
- }
- @using WebApplication1.Models
- @{
- var studentsList = Model as List<Student>;
- var data = ViewBag.Data;
- }
- @{Response.Write(data); }
- <table border="1px solid" cellpadding="0" cellspacing="0">
- @foreach (Student student in studentsList)
- {
- <tr>
- <th>@student.Name</th>
- <th>@student.Gender</th>
- <th>@student.Age</th>
- <th>@student.Phone</th>
- </tr>
- }
- </table>
三、RenderPage
1.Razor語法:@RenderPage()2.區別:也可以使用 RenderPage 來呈現部分,但它不能使用 原來檢視的 Model 和 ViewData ,只能通過引數來傳遞。而 RenderPartial、RenderAction 可以使用原來檢視的 Model 和 ViewData。@RenderPage也並沒有執行Action。
3.例項:
不傳引數情況:
Index.cshtml:
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <h3>我是首頁</h3>
- <section>
- <h4>分部檢視</h4>
- @RenderPage("~/Views/Templates/Partial1.cshtml")
- </section>
- </div>
- </body>
- </html>
Index.cshtml:
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <h3>我是首頁</h3>
- <section>
- <h4>分部檢視</h4>
- @RenderPage("~/Views/Templates/Partial1.cshtml",new { param1="longxi",param2="男"})
- </section>
- </div>
- </body>
- </html>
- @{
- var param = string.Format("{0}-{1}", PageData["param1"], PageData["param2"]);
- }
- @Html.Raw(param)