論MVC中的傳值

來源:互聯網
上載者:User

標籤:io   ar   for   sp   on   cti   html   bs   new   

   2個頁面分別為Father.cshtml、Child.cshtml

   2個控制器分別為FatherController、ChildController

   2個js分別為Father.js、Child.js

 

一、FatherController傳值給Father.cshtml:

  1.ViewBag 和 ViewData

    傳遞:   ViewBag.param1 = "hello";    // ViewData["param1"] = "hello";

    接收:  @ViewBag.param1   //@ViewData["param1"] = "hello"

    ViewBag 不再是字典的索引值對結構,而是 dynamic 類型,它會在程式啟動並執行時候動態解析。

    ViewBag和ViewData互連,僅針對當前Action中有效,生命週期和View相同。

    ViewBag可以直接被foreach,ViewData不行。

  2.TempData

    傳遞:   TempData["param1"]="hello";

    接收:   @TempData["param1]

    TempData至多隻能經過一次Controller,並且每個元素至多隻能被訪問一次,之後就會被刪除。

         本質是存在Session中。

         一般用於緩衝或者拋出錯誤提示。

 

  3.Model

    傳遞: Person entity = new Person()  //事先定義好Person類

       {

        Name="jerry",Age=18;

       }

       return View(entity);  //如果返回視圖名字就 return View("Index",entity);

    接收:  @Model.Name   @Model.Age

  

二、Father.cshtml傳值給FatherController:

  to be continued...

論MVC中的傳值

聯繫我們

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