HtmlHelper

@Html.Action("Search") (因為影響版面,所以暫時註解掉了)

@Html.Action 會呼叫 Controller 中 Action 來回傳 View 畫面至主檢視頁面中,經歷完整生命週期( Request Life Cycle), 也就是當顯示區塊資料需從 Controller-Action 運算後獲得時,可使用此方式插入頁面。我們可以在Controller中使用ParitalView 來回傳檢視,讓傳回的View不套用預設母版頁面,其效果相當於 Layout = null 設定; 此外可以透過 ChildActionOnly 標籤來標記限制此 Action 不可被外部直接訪問 .

用Html.Action 載入Search的完整html (包含 layout , heder 部份因為 css 的關係與 主頁的疊在一起了, 但在原始檔看得到)

@Html.Partial("Search")

@Html.Partial 則是直接調用指定 .cshtml 檢視內容輸出至主檢視頁面中。

用Html.Partial 載入Search的 不包含 layout , 只有 content

可在參數裡代入

我是被載入的Action [Search]

查詢
IndexName2
IndexName

Html.Action 及 Html.Partial 的限制

要達成以上條件必須 .cshtml 不可指定 Layout, 而且 controller 裡的 action 是指定回傳 ActionResult 的

  • .cshtml 不可指定 Layout , 如果指定的話不管是 Action 或 Partial 都會載入完整內容
  • controller 裡的 action 如果指定回傳 PartialViewResult 的話不管是 Action 或 Partial 都只會載入成 PartialView 內容 (也就是不含 Layout 的內容)