Summary of data transfer between controller and view in ASP.

Source: Internet
Author: User

in ASP. NET MVC, the data is often passed between the controller and the view, so it is important to master the data transfer method between the two layers skillfully and flexibly. This paper discusses from two aspects:

Ø Controller transmits data to View

Ø View transmits data to Controller

First, Controller to View Passing Data


1. Passing Data using ViewData

We define this in the controller as follows:

viewdata["Message"] = "Hello word!";

The ViewData data defined in the controller is then read in the view, with the following code:

<% = Html.encode (viewdata["Message"])%>

2. Passing Data using tempdata

We define this in the controller as follows:

tempdata["Message"] = "Hello word!";

The TempData data defined in the controller is then read in the view, with the following code:

<% = Html.encode (TempData ["Message"])%>

3. Passing Data using Model

When using model to pass data, we typically create a view with the option of creating a strongly typed view as shown:

After creating a strongly typed view, the first line of view is as follows:

<%@ page title= "" Language= "C #" masterpagefile= "~/views/shared/site.master" inherits= "System.Web.Mvc.ViewPage <MvcInduction.Models.People> "%>

<MvcInduction.Models.People> represents this view using the model "MvcInduction.Models.People"

Summarize:

1. ViewData and TempData are weakly typed ways of transmitting 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, and the controller fetches the TempData data from the session each time the request is executed and deletes the session. 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.

Second, View to Controller Passing Data

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

We make the following definitions in the view layer:

<% using (Html.BeginForm ("ActionName", "Controllername"))

{%>

username:<% Html.textbox ("UserName"); %>

password:<% Html.textbox ("Password"); %>

<%}%>

Note: ActionName is the corresponding action name and Controllername is the corresponding controller name

Then at the controller layer, the code that reads the form data through Request.Form is as follows:

[Acceptverbs (Httpverbs.post)]

Public ActionResult ActionName ()

{

String username = request.form["username"];

string password = request.form["password"];

return View ();

}

2. reading form data via FormCollection

We make the following definitions in the view layer:

<% using (Html.BeginForm ("ActionName", "Controllername"))

{%>

username:<% Html.textbox ("UserName"); %>

password:<% Html.textbox ("Password"); %>

<%}%>

Then at the controller layer, the code that reads the form data through FormCollection is as follows:

[Acceptverbs (Httpverbs.post)]

Public ActionResult ActionName (formcollection formcollection)

{

String username = formcollection["username"];

string password = formcollection["password"];

return View ();

}

3. Custom Data binding


The way to customize data binding is to create a custom data-binding class that inherits the class from Imodelbinder and implements the Bindmodel method in that interface.
The code is not listed because of writing haste. Please forgive me.

Summary: Although we can read form data by Request.Form or formcollection, it is often cumbersome to use the built-in approach to the Controller base class in the case of strongly typed view Updatemodel (), which supports updating an object's properties with an incoming form parameter, uses a reflection mechanism to resolve the object's property name, and then automatically assigns the object-related properties based on the parameter values passed in by the client.

Here is an example of a demo that I wrote using the Updatemodel code:

example of code using Updatemodel ()[Acceptverbs (Httpverbs.post)]
Public ActionResult Edit (int ID, formcollection collection)
{
Users user = Userrepository.getuser (ID);
User. UserName = request.form["UserName"];
User. Password = request.form["Password"];
User. Telephone = request.form["Telephone"];
User. Address = request.form["Address";
The above method is a bit cumbersome, especially after adding exception handling logic. A better approach is to use the built-in method of the Controller base class Updatemodel (). This method supports updating the properties of an object using the incoming form parameter, which uses the reflection mechanism to parse the object's property name, and then automatically assigns the object-related properties based on the parameter values passed in by the client.
Users user = Userrepository.getuser (ID);
string[] Allowedproperties = new[] {"UserName", "Password", "Telephone", "Address"};
Updatemodel (user, allowedproperties);
Userrepository.save ();

Return redirecttoaction ("Details", new {id = user.id});
}(This article turned from: http://www.cnblogs.com/wlb/archive/2009/12/10/1621475.html) Category: ASP.

Summary of 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.