1. 程式人生 > >MVC |分部視圖 PartialView()

MVC |分部視圖 PartialView()

mode new inf bin pop 自定義 index ner --

介紹如何定義

其實它和普通視圖沒有多大區別,只是創建分部視圖的時候視圖裏沒有任何內容,你需要什麽標簽你自己加。第二就是分部視圖不會執行_ViewStart.cshtml中的內容)

控制器

PartialViewDeomController控制器

[csharp] view plain copy
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApp.Controllers
  7. {
  8. public class PartialViewDeomController : Controller
  9. {
  10. //
  11. // 分部視圖的作用一般用於嵌到如一些正常的視圖中去。(類似與自定義控件)相當於: Server.Execute(string path)
  12. public ActionResult PartialIndex()
  13. {
  14. //View()方法返回的視圖默認都會去執行_ViewStart.cshtml中的內容
  15. //return View();
  16. //分部視圖不會去執行_ViewStart.cshtml中的內容(分部視圖以PartialView()返回)
  17. return PartialView();
  18. }
  19. }
  20. }


PartialIndex 視圖

[html] view plain copy
  1. <!--註意,創建分部視圖後,視圖裏是沒有任何東西的。自己需要什麽標簽,就加什麽標簽。這個視圖的用法就是到時候嵌套到一些以View()返回的正常視圖中去-->
  2. <select id="dp1">
  3. <option value="0">湖南</option>
  4. <option value="1">廣東</option>
  5. <option value="0">上海</option>>
  6. </select>
  7. <select id="dp2">
  8. <option value="0">紐約</option>
  9. <option value="1">洛杉磯</option>
  10. <option value="0">華盛頓</option>>
  11. </select>


技術分享

使用介紹(我們發現它與普通視圖是差不多的)

PartialViewDeomController控制器

[csharp] view plain copy
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApp.Controllers
  7. {
  8. using MvcApp.Models;
  9. public class PartialViewDeomController : Controller
  10. {
  11. //
  12. // 分部視圖的作用一般用於嵌到如一些正常的視圖中去。(類似與自定義控件)相當於: Server.Execute(string path)
  13. public ActionResult PartialIndex()
  14. {
  15. var list = new List<T_UserInfo>()
  16. {
  17. new T_UserInfo(){Id=1,UserName="無鹽海",Name="凡斌"},
  18. new T_UserInfo(){Id=1,UserName="阿寶",Name="周晶"},
  19. };
  20. //分部視圖不會去執行_ViewStart.cshtml中的內容(分部視圖以PartialView()返回)
  21. return PartialView(list);
  22. }
  23. }
  24. }


PartialIndex視圖

[html] view plain copy
  1. @model List<MvcApp.Models.T_UserInfo>
  2. <select id="dp1">
  3. @{
  4. foreach (var item in Model)
  5. {
  6. <option value="0">@item.Name</option>
  7. }
  8. }
  9. </select>

真實的使用介紹 (重點)

PartialViewDeomController控制器

[csharp] view plain copy
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace MvcApp.Controllers
  7. {
  8. using MvcApp.Models;
  9. public class PartialViewDeomController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. return View();
  14. }
  15. // 分部視圖的作用一般用於嵌到如一些正常的視圖中去。(類似與自定義控件)相當於: Server.Execute(string path)
  16. public ActionResult PartialIndex()
  17. {
  18. var list = new List<T_UserInfo>()
  19. {
  20. new T_UserInfo(){Id=1,UserName="無鹽海",Name="凡斌"},
  21. new T_UserInfo(){Id=1,UserName="阿寶",Name="黃雪輝"},
  22. };
  23. //分部視圖不會去執行_ViewStart.cshtml中的內容(分部視圖以PartialView()返回)
  24. return PartialView(list);
  25. }
  26. }
  27. }


Index視圖與PartialIndex分部視圖。(註意:這裏是在Index視圖裏調用PartialIndex分部視圖)

[html] view plain copy
    1. @{
    2. Layout = null;
    3. }
    4. @using MvcApp.Models;
    5. <!DOCTYPE html>
    6. <html>
    7. <head>
    8. <meta name="viewport" content="width=device-width" />
    9. <title>Index</title>
    10. <script src="~/Scripts/jquery-1.8.2.js"></script>
    11. </head>
    12. <body>
    13. <div id="loadData"></div>
    14. <div>
    15. <!--第一種方式:同一控制起下調用分部視圖-->
    16. @Html.Partial("PartialIndex", new List<T_UserInfo>() { new T_UserInfo() { Id = 1, UserName = "無鹽海", Name = "凡斌" }, new T_UserInfo() { Id = 1, UserName = "阿寶", Name = "周晶" }, })
    17. <!--第二種方式:同一控制起下調用分部視圖-->
    18. @{
    19. Html.RenderPartial("PartialIndex", new List<T_UserInfo>() { new T_UserInfo() { Id = 1, UserName = "無鹽海", Name = "凡斌" }, new T_UserInfo() { Id = 1, UserName = "阿寶", Name = "周晶" } });
    20. }
    21. <!--第三種方式:可以跨控制器調用分部視圖(註意:如果通過這種方式調用分部視圖,如果在再PartialIndex這個action中有傳參給分部視圖,則在此處調用就不需要再傳遞參數了)-->
    22. @{Html.RenderAction("PartialIndex", "PartialViewDeom");}
    23. <!--第四種方式:也是可以跨控制器調用分部視圖。和第三種是一樣的-->
    24. @Html.Action("PartialIndex", new { controller = "PartialViewDeom" })
    25. <!--第五種方式:用ajax來調用:如:jquery的Load()方法-->
    26. <script type="text/javascript">
    27. $(function () {
    28. $("#loadData").load("/PartialViewDeom/PartialIndex"); //將PartialIndex分部視圖中的內容加載到id為loadData這個元素中去
    29. })
    30. </script>
    31. </div>action
    32. </body>
    33. </html>

MVC |分部視圖 PartialView()