Data transfer between Controller and view in ASP.

Source: Internet
Author: User

in ASP. NET MVC, data is often passed between the controller and the view

1. controller transmits data to view

(1) Use viewdata["user"]

(2) using Viewbag.user

(3) Use tempdata["user"]

(4) Use model (strong type)

Difference:

(1) ViewData and TempData mode is a weakly typed way to pass data, while using model to pass data is a strongly typed way.

(2) ViewData and TempData are completely different data types, ViewData data types are instanced objects of the Viewdatadictionary class, and TempData data types are instanced objects of the Tempdatadictionary class.

(3) TempData is actually saved in the session, the controller fetches the TempData data from the session and deletes the session each time it requests it. TempData data can only be passed once in the controller, each of which can only be accessed once, and is automatically deleted after access.

(4) ViewData can only be set in one action method and read on the relevant view page, only valid for the current view. Theoretically, TempData should be able to be set in one action and read multiple pages. However, the elements in the tempdata are actually accessed once and then deleted.

(5) The difference between ViewBag and ViewData: ViewBag is no longer the key-value pair structure of a dictionary, but dynamic type, which is parsed dynamically when the program is running.

2. View transmits data to controller

in ASP. NET MVC, the data in the view is passed to the controller, which is implemented primarily by sending forms. The specific ways are:

(1) Reading form data via Request.Form

View Layer:

 @using (Html.BeginForm ( " hellomodeltest  , "   Home   , FormMethod.Post)) {@ Html.textbox (  name   " ); @Html. TextBox (  text   " );  <input type= submit   " Value="   commit   "/>"  

Controller:

     [HttpPost]        public  actionresult hellomodeltest ()        {            string Name= request.form["Name"];             string text= request.form["text"];             return View ();        }

(2) Reading form data via FormCollection

[HttpPost]          Public ActionResult hellomodeltest (formcollection FC)        {            string name= fc["name"];             string Text  = fc["text"];             return View ();        }

(3) Binding by model

1), direct the model of the page.

[HttpPost]  Public ActionResult hellomodeltest (Hellomodel model) {    //  ...}

2), the element of the page must have the Name property equal to the name and text of the

 Public string name,string  text) {    //  ...}

(4) through Ajax, but the parameters passed inside the controller are identical to the parameter names

Data transfer between Controller and view in ASP.

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.