Passing values through MVC and passing values through mvc

Source: Internet
Author: User

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 ,....})


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.