Passing values through MVC and passing values through mvc
1,ViewBag
Controller: ViewBag. Message = "Hello, Word ";
View: @ ViewBag. Message
Note: The ViewBag type is dynamic. You can directly use it if you are not sure. Its value range is: the controller transfers values to the view, and the view itself and its own values.
2,ViewData
Controller: ViewData ["message" "] =" Hello, Word ";
View: string message = ViewData ["message"] as string;
@ Message
Note: The ViewData type is very clear. Forced type conversion is often required when used. The value range is: the controller transfers values to the view, and the view itself and its own values.
3,TempData
Controller: TempData ["message"] = "Hello, Word ";
Controller: if ("Hello, Word" = TempData ["message"] as string) {TempData ["message"] = "Hello! ";}
View: string message = TempData ["message"] as string;
@ Message
Note: The purpose of TempData is to prevent data loss during redirect (ViewData and ViewBag will become null after jump, but TempData will not ), its value range is between the current controller and the controller after the jump.
4,Passing model on a common page
Controller: StarModel p = new StarModel (); p. Name = "Suk"; return View (p );
View: <%: (StarModel) Model). Name %>
5,Try to pass model to strong type
A: (implementation of WebForm)
Controller: StarModel p = new StarModel (); p. Name = "Suk"; return View (p );
View: <% @ Page Inherits = "System. Web. Mvc. ViewPage <StarModel>" %>
<%: Model. Name %>
B: (implementation of Razor)
Controller: StarModel p = new StarModel (); p. Name = "Suk"; return View (p );
View: @ model MOTest. Models. StarModel
@ Model. Name
6,ViewModel
Controller: var StarModelTest = new StarModel (p); return View (StarModelTest );
View: @ model MOTest. Models. StarModelTest
@ Model. Name
7,RedirectToAction
A: (passing object classes)
RedirectToAction (controller, Controller method, entity class)
B: (passing strings)
RedirectToAction (controller, Controller method, new {name = value ,....})