The--view between the "MVC framework" and the controller

Source: Internet
Author: User

In MVC, the controller executes a routing function that uses the data from the view to determine which model should be called, as well as to pass the model processing data to the view, so it always involves the controller and view values. So how do they pass the value between them?


Controller passes value to view


1. Using ViewBag


Controller

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public ActionResult Index () {     viewbag.message = "Welcome to ASP. NET mvc!";     return View ();} </span>


View

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >@{    viewbag.title = "Home";} 
The message sent by VIEWBAG will be passed to



2. Using ViewData


Controller

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public actionresult Index ()    {        viewdata["Message"] = "Welcome to ASP. mvc!";        return View ();    } </span>


View

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >
The effect is the same.


3. Using TempData


Controller

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public actionresult Index ()    {        tempdata["Message"] = "Welcome to ASP. mvc!";        return View ();    } </span>


View

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >


4. Use model


Controller

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >public actionresult Modeldemo ()      {          user u= new User () {username= "li", password= "ABCDE"};          return View (U);      }   </span>


View

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><p>         <%user u = (User) viewdata.model;%>         UserName:          <%= html.encode (u.username)%>      </p>      <p>          Password:           <%= html.encode (u.password)%>       </p>    </ Span>


Here are the different points of the four methods:

ViewData is the Key/value dictionary collection, in the MVC1, there is the ViewData value is faster than ViewBag, ViewBag is a dynamic type object, starting from MVC3, is slower than viewdata, but good readability.


ViewData can only be used in the current action, while TempData is similar to a session and can be accessed across actions and is typically used to store error messages.


The model passes a strongly typed, so when you create a view, you create a strong view.


View to controller pass value


1. Read form data via Request.Form


View

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><% using (Html.BeginForm ("ActionName", "Controllername"))       {%>    username:<% html.textbox (" UserName "); %>    password:<% html.textbox ("Password");%><%}%></span>

Controller

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >[acceptverbs (Httpverbs.post)] public        actionresult actionname ()        {             string username = request.form[" UserName "];            string password = request.form["password"];            return View ();} </span>

2. Read form data via FormCollection


View

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><% using (Html.BeginForm ("ActionName", "Controllername"))       {%>    username:<% html.textbox (" UserName "); %>    password:<% html.textbox ("Password");%><%}%></span>


Controller

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >[acceptverbs (Httpverbs.post)] public        actionresult actionname (formcollection formcollection)        {            String username = formcollection["username"];            string password = formcollection["password"];            return View ();        } </span>


Summarize

Page Pass value will use a variety of methods, then the page and the controller between the value of the same can be a lot of methods. The value of view and controller is unavoidable, and mastering the method of transmission between them is helpful to develop more fluently.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MVC Framework--view and controller pass-through values

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.