MVC |分部視圖 PartialView()
阿新 • • 發佈:2017-06-15
mode new inf bin pop 自定義 index ner --
介紹如何定義
其實它和普通視圖沒有多大區別,只是創建分部視圖的時候視圖裏沒有任何內容,你需要什麽標簽你自己加。第二就是分部視圖不會執行_ViewStart.cshtml中的內容)
控制器
PartialViewDeomController控制器
[csharp] view plain copy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcApp.Controllers
- {
- public class PartialViewDeomController : Controller
- {
- //
- // 分部視圖的作用一般用於嵌到如一些正常的視圖中去。(類似與自定義控件)相當於: Server.Execute(string path)
- public ActionResult PartialIndex()
- {
- //View()方法返回的視圖默認都會去執行_ViewStart.cshtml中的內容
- //return View();
- //分部視圖不會去執行_ViewStart.cshtml中的內容(分部視圖以PartialView()返回)
- return PartialView();
- }
- }
- }
PartialIndex 視圖
[html] view plain copy
- <!--註意,創建分部視圖後,視圖裏是沒有任何東西的。自己需要什麽標簽,就加什麽標簽。這個視圖的用法就是到時候嵌套到一些以View()返回的正常視圖中去-->
- <select id="dp1">
- <option value="0">湖南</option>
- <option value="1">廣東</option>
- <option value="0">上海</option>>
- </select>
- <select id="dp2">
- <option value="0">紐約</option>
- <option value="1">洛杉磯</option>
- <option value="0">華盛頓</option>>
- </select>
使用介紹(我們發現它與普通視圖是差不多的)
PartialViewDeomController控制器[csharp] view plain copy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcApp.Controllers
- {
- using MvcApp.Models;
- public class PartialViewDeomController : Controller
- {
- //
- // 分部視圖的作用一般用於嵌到如一些正常的視圖中去。(類似與自定義控件)相當於: Server.Execute(string path)
- public ActionResult PartialIndex()
- {
- var list = new List<T_UserInfo>()
- {
- new T_UserInfo(){Id=1,UserName="無鹽海",Name="凡斌"},
- new T_UserInfo(){Id=1,UserName="阿寶",Name="周晶"},
- };
- //分部視圖不會去執行_ViewStart.cshtml中的內容(分部視圖以PartialView()返回)
- return PartialView(list);
- }
- }
- }
PartialIndex視圖
[html] view plain copy
- @model List<MvcApp.Models.T_UserInfo>
- <select id="dp1">
- @{
- foreach (var item in Model)
- {
- <option value="0">@item.Name</option>
- }
- }
- </select>
真實的使用介紹 (重點)
PartialViewDeomController控制器
[csharp] view plain copy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcApp.Controllers
- {
- using MvcApp.Models;
- public class PartialViewDeomController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- // 分部視圖的作用一般用於嵌到如一些正常的視圖中去。(類似與自定義控件)相當於: Server.Execute(string path)
- public ActionResult PartialIndex()
- {
- var list = new List<T_UserInfo>()
- {
- new T_UserInfo(){Id=1,UserName="無鹽海",Name="凡斌"},
- new T_UserInfo(){Id=1,UserName="阿寶",Name="黃雪輝"},
- };
- //分部視圖不會去執行_ViewStart.cshtml中的內容(分部視圖以PartialView()返回)
- return PartialView(list);
- }
- }
- }
Index視圖與PartialIndex分部視圖。(註意:這裏是在Index視圖裏調用PartialIndex分部視圖)
[html] view plain copy
- @{
- Layout = null;
- }
- @using MvcApp.Models;
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- <script src="~/Scripts/jquery-1.8.2.js"></script>
- </head>
- <body>
- <div id="loadData"></div>
- <div>
- <!--第一種方式:同一控制起下調用分部視圖-->
- @Html.Partial("PartialIndex", new List<T_UserInfo>() { new T_UserInfo() { Id = 1, UserName = "無鹽海", Name = "凡斌" }, new T_UserInfo() { Id = 1, UserName = "阿寶", Name = "周晶" }, })
- <!--第二種方式:同一控制起下調用分部視圖-->
- @{
- Html.RenderPartial("PartialIndex", new List<T_UserInfo>() { new T_UserInfo() { Id = 1, UserName = "無鹽海", Name = "凡斌" }, new T_UserInfo() { Id = 1, UserName = "阿寶", Name = "周晶" } });
- }
- <!--第三種方式:可以跨控制器調用分部視圖(註意:如果通過這種方式調用分部視圖,如果在再PartialIndex這個action中有傳參給分部視圖,則在此處調用就不需要再傳遞參數了)-->
- @{Html.RenderAction("PartialIndex", "PartialViewDeom");}
- <!--第四種方式:也是可以跨控制器調用分部視圖。和第三種是一樣的-->
- @Html.Action("PartialIndex", new { controller = "PartialViewDeom" })
- <!--第五種方式:用ajax來調用:如:jquery的Load()方法-->
- <script type="text/javascript">
- $(function () {
- $("#loadData").load("/PartialViewDeom/PartialIndex"); //將PartialIndex分部視圖中的內容加載到id為loadData這個元素中去
- })
- </script>
- </div>action
- </body>
- </html>
MVC |分部視圖 PartialView()