【ASP.NET MVC 學習筆記】- 13 Child Action

來源:互聯網
上載者:User

標籤:視圖   turn   參數   result   list   font   new   cti   傳值   

本文參考:http://www.cnblogs.com/willick/p/3410855.html

1、Child action 和 Patial view 類似,也是在應用程式的不同地方可以重複利用相同的子內容。不同的是,它是通過調用 controller 中的 action 方法來呈現子內容的,並且一般包含了業務的處理。任何 action 都可以作為子 action 。樣本:

//1、ChildActionOnly 特性保證了該 action 只能作為子action被調用(不是必須的)[ChildActionOnly]public ActionResult Time(){    return PartialView(DateTime.Now);}//2、View Time.cshtml@model DateTime<p>The time is: @Model.ToShortTimeString()</p> //3、在 List.cshtml 視圖中添加如下代碼來調用 Time action 方法 :@Html.Action("Time")

    通過 @Html.Action 方法來調用了 Time action 方法來呈現子內容。在這個方法中我們只傳了一個action名稱參數,MVC將根據當前View所在Controller去尋找這個action。如果是調用其它 controller 中的 action 方法,則需要在第二個參數中指定 controller 的名稱,如下:

@Html.Action("Time", "MyController")

    該方法也可以給 action 方法的參數傳值,如對於下面帶有參數的 action:

[ChildActionOnly] public ActionResult Time(DateTime time) {     return PartialView(time); }@Html.Action("Time", new { time = DateTime.Now })

 

【ASP.NET MVC 學習筆記】- 13 Child Action

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.