Transferred from: http://www.cnblogs.com/zhuqil/archive/2010/08/03/Passing-Data-from-Controllers-to-View.html
in ASP. NET MVC development, the controller needs to provide the model to the view, and then the view renders the model as HTML. This article introduces three ways to pass data from a controller to a view to achieve a DropDownList display.
The first type: ViewData
ViewData is a dictionary. The use is very simple, see the following code:
1 public actionresult viewdataway (int id)
2 {
3 Book book =bookrepository.getbook (ID);
4 viewdata["countries"] = new SelectList (phonevalidator.countries, book. Country);
5 return View (book);
4?
Use the following code in the view to take the value:
1 <div class= "Editor-field" >
2 <%= html.dropdownlist ("Country", viewdata["countries"] as SelectList)%>
3 <%: html.validationmessagefor (model = model. Country)%>
4 </div>
The above code uses as to convert it to selectlist.
The post code is processed as follows:
1 [HttpPost]
2 public actionresult viewdataway (int ID, formcollection collection)
3 {
4 Book book = Bookrepository.getbook (ID);
5 updatemodel<book> (book);
6 Bookrepository.save (book);
7 return redirecttoaction ("Details", new {id=id});
8}
Effect:
The second type: ViewModel
Using ViewModel, we first create a bookviewmodel with the following code:
1 public class Bookviewmodel
2 {
3 Public Book Book
4 {
5 get;
6 set;
7}
8
9 Public SelectList countries
10 {
One get;
Set;
13}
Public Bookviewmodel
15 {
book = Book;
Countries = new SelectList (Phonevalidator.countries,book. Country);
18}
19}
The code for storing data in the controller's Aciton using ViewModel is as follows:
1 public actionresult viewmodelway (int id)
2 {
3 Book book = Bookrepository.getbook (ID);
4 return View (new Bookviewmodel);
5}
In view, this approach is better than the first: it supports intelligent sensing.
The effect is the same as in the first way.
The third type: TempData
Using TempData is the same as using the ViewData method.
The action code is as follows:
1 public actionresult tempdataway (int id)
2 {
3 Book book = Bookrepository.getbook (ID);
4 tempdata["countries"] = new SelectList (phonevalidator.countries, book. Country);
5 return View (book);
6}
The code for the view value is as follows:
1 <div class= "Editor-field" >
2 <%= html.dropdownlist ("Country", tempdata["countries"] as SelectList)%>
3 <%: html.validationmessagefor (model = model. Country)%>
4 </div>
Effect: the first way.
The difference between TempData and ViewData
Take a simple test and look at the difference between TempData and ViewData.
1
2 public ActionResult Test1 ()
3 {
4
5 tempdata["text"] = "the";
6 viewdata["text"] = "the";
7 return redirecttoaction ("Test2");
8
9
10}
11
12
Public ActionResult Test2 ()
14 {
15
String Text1 = tempdata["text"] as String;
Text2 string = viewdata["text"] as String;
return View ();
19
20}
See below, found after Test1 through redirecttoaction jump Test2, the value of ViewData has been emptied, and tempdata is not emptied, this is one of their differences, We can use TempData to pass data between controllers.
About the difference between TempData and ViewData, found that most of the online feeling is not right, there is a kind of saying:
1.TempData data can only be passed once by the controller, and each element can be accessed at most once .
2. The elements in TempData will be deleted once they are accessed once.
I try to find that both TempData and ViewData are only valid on a single request and fail at the time of the second request. TempData can pass data between multiple controllers in a single request, but ViewData does not, to prove it. TempData can also be accessed multiple times. It should be said that MVC has an action at the end of the request cycle to delete such a session, rather than deleting it once. Ms named TempData, meaning that TempData is a session, but it is different from the normal session. It will be deleted after the request, so it is a temporary data.
Three ways in which controller passes data to view in ASP. NET MVC2