asp.net MVC 4.0 View回顧——布局頁與分部頁

來源:互聯網
上載者:User

標籤:style   blog   class   c   code   java   

asp.net MVC 4.0中總結 視圖裡載入部分視圖幾種方法

@RenderPage()

但它不能使用 原來視圖的 Model 和 ViewData ,只能通過參數來傳遞。

1 @RenderPage("~/Shared/Component/Dialog.cshtml", new { title = "Hello world!", content="Nani" })

 分部視圖接收資料通過Page

1 <div id="dialog" title="@Page.title" style="display: none;">2     <p>3         @Page.title4     </p>5 </div>

 

@Html.Partial()

用於將分部視圖渲染為字串

@Html.Partial("_PartialPage1",model,ViewData)直接返回MvcHtmlString填充

1 @Html.Partial("Component/Dialog", null, new ViewDataDictionary { { "title", "Hello world!" }, { "content", "Nani?" } })

Razor子視圖裡使用 ViewBag 來擷取傳遞的資料

1 <div id="dialog" title="@ViewBag.title" style="display: none;">2     <p>3         @ViewBag.content4     </p>5 </div>

 傳遞強型別到部分視圖

1 @{2  var args = new Dictionary<string,string>();3  args["redirectController"] = "Admin";4  args["redirectAction"] = "User";5 }6  @Html.Partial("_childPartial",args)

_childPartial.cshtml

1 @model Dictionary<string,string>2 <div>@Model["redirectController"]</div>3 <div>@Model["redirectAction"]</div>

 

@RenderPartial()

將分布視圖直接寫入響應輸出資料流,所以只能直接放在代碼塊中,不能放在運算式中(傳回值是void)

RenderPartial 不需要建立 Controller 的 Action ,而 RenderAction 需要在 Controller 建立要載入的 Action。RenderAction 會先去調用 Contorller 的 Action ,最後再 呈現視圖,所以這裡 頁面會在 發起一個連結。如果這個部分視圖只是一些簡單 的 html 代碼,請使用 RenderPartial。 但如果這個部分視圖 除了有 html 代碼外,還需要 通過 讀取資料庫裡的資料 來渲染,就必須使用 RenderAction 了,因為 它可以在 Action 裡調用 Model裡的 法讀取資料庫,渲染視圖後在呈現,而 RenderPartial 沒有 Action,所以無法做到。

Partial 可以直接輸出內容,它內部是 將 html 內容轉換為 string 字元(MVCHtmlString),然後緩衝起來,      最後在一次性輸出到頁面。顯然,這個轉換的過程,會降低效率,所以通常使用 RenderPartial 代替。


@Html.Action()

實體類

 1 public class Menu 2 { 3     public List<MenuItem> Items { get; set; } 4 } 5  6 public class MenuItem 7 { 8     public string Text { get; set; } 9     public string Url { get; set; }10 }

控制器

 1 public ActionResult MyMenu() 2 { 3    MvcLearn.Models.Menu m = new MvcLearn.Models.Menu(); 4    List<MvcLearn.Models.MenuItem> items = new List<MenuItem>(); 5    items.Add( new MenuItem(){ Text = "Baidu", Url = "http://www.baidu.com"}); 6    items.Add(new MenuItem() { Text = "Sina", Url = "http://www.Sina.com" }); 7    items.Add(new MenuItem() { Text = "IBM", Url = "http://www.ibm.com" }); 8    items.Add(new MenuItem() { Text = "Sohu", Url = "http://www.sohu.com" }); 9    m.Items = items;10    return PartialView(m);11 }

建立一個PartialView - MyMenu.cshtml

1 @model MvcLearn.Models.Menu  2 <ul>  3 @foreach(var item in Model.Items)  4 {  5     <li><a href="@item.Url">@item.Text</a></li>  6 }  7 </ul> 

在頁面中調用該Action產生視圖:

 1 @Html.Action("MyMenu") 

調用Action時傳入參數

1 @Html.Action("MyMenu", new { mi= new MvcLearn.Models.MenuItem(){ Text = "haha", Url =<a href="http://www.ms.com">http://www.ms.com</a>}})


@Html.RenderAction()

 1 @{Html.RenderAction("MyMenu")} 

Action 也是直接輸出,和 Partial 一樣,也存在一個轉換的過程。不如 RenderAction 直接輸出到 當前HttpContext 的效率高。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.