MVC基礎知識-Controller

來源:互聯網
上載者:User

標籤:

建立xxController繼承自:System.Web.Mvc.Controller

一個Controller可以包含多個Action. 每一個Action都是一個方法, 返回一個ActionResult執行個體

一個Controller對應一個xxController.cs控制檔案,對應在View中有一個xx檔案夾,用於存放xxController下Action對應的View檔案。一般情況一個Action對應一個View頁面

 

Action方法輸出傳回值:

1.string:直接 返迴響應報文字串;

2.ActionResult:返回控制器結果對象

3.JsonResult:返回 Json 字串

 

控制器將處理後的資料“傳”給視圖的方式

ViewData/ViewBag/TempData/Model

public ActionResult Index()
{
Employee data = new Employee();
data.Name = "張三";
data.Age = 26;
ViewBag.UserName = "白子畫";
ViewData["UserName"] = "花千骨";
TempData["UserName"] = "殺阡陌";//臨時資料
return View(data); //這行代碼其實就相當於ViewData.Model=model
}

輸出結果為:

Model其實就是ViewData.Model

 

第一個ViewBag.UserName 將被 ViewData["UserName"] 覆蓋,最後的結果不會輸出“白子畫”

因為ViewData和ViewBag本質上都是【ViewDataDictionary】類型,並且兩者之間的資料共用,只不過提供了不同的文法操作方式而已

Controller傳資料到View

給Controller中的ViewData/ViewBag/TempData/賦值了,或者把對象傳給了View方法

View和Controller中都有ViewData/ViewBag/TempData這幾個對象,在給Controller中這些對象賦值後,Controller會把這些值賦值給View中對應的這幾個對象


 

MVC基礎知識-Controller

聯繫我們

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