In ASP. NET MVC2, the Controller transmits data to the view in three ways.

Source: Internet
Author: User

In Asp.net MVC development, the controller needs to provide a model to the view, and then the view renders the model into HTML. This article introduces three ways for the Controller to transmit data to the view to display a dropdownlist.

First: viewdata

Viewdata is a dictionary. The usage 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);
6 }

Use the following code in view:

 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 processing code is 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:

Type 2: viewmodel

To use viewmodel, create a bookviewmodel first. The Code is as follows:

  1     public class BookViewModel
2 {
3 public Book Book
4 {
5 get;
6 set;
7 }
8
9 public SelectList Countries
10 {
11 get;
12 set;
13 }
14 public BookViewModel(Book book)
15 {
16 Book = book;
17 Countries = new SelectList(PhoneValidator.Countries,book.Country);
18 }
19 }

The code for using viewmodel to store data in the Controller's aciton is as follows:

 1         public ActionResult ViewModelWay(int id)
2 {
3 Book book = bookRepository.GetBook(id);
4 return View(new BookViewModel(book));
5 }

In view, this method is better than the first one: it supports intelligent sensing.

The effect is the same as that of the first method.

Third: tempdata

The use of tempdata is the same as the use of viewdata.

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 viewing values 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 method is the same.

Difference between tempdata and viewdata

Let's take a simple test to see the difference between tempdata and viewdata.

  1 
2 public ActionResult Test1()
3 {
4
5 TempData["text"] = "1-2-3";
6 ViewData["text"] = "1-2-3";
7 return RedirectToAction("Test2");
8
9
10 }
11
12
13 public ActionResult Test2()
14 {
15
16 string text1 = TempData["text"] as string;
17 string text2 = ViewData["text"] as string;
18 return View();
19
20 }

Looking at the following, we found that after test1 jumps to Test2 through redirecttoaction, The viewdata value has been cleared, while tempdata has not been cleared. This is one of their differences, we can use tempdata to transmit data between controllers.

Regarding the difference between tempdata and viewdata, I found that most of the online statements are not correct. There is a saying on the Internet:

1,Tempdata data can only be transmitted once by the Controller, and each element can only be accessed once at most..

2. Elements in tempdata will be deleted after being accessed once.

I tried it and found that both tempdata and viewdata will only be valid in one request and will not expire in the second request. Tempdata can transmit data between multiple controllers in a request, but viewdata cannot, which can be proved. Tempdata can also be accessed multiple times. It should be said that MVC has an action to delete such sessions at the end of the Request cycle, instead of being deleted after one access. Ms is named tempdata, which means that tempdata is a session, but it is different from a common session. It will be deleted after the request, so it is temporary data.

Summary:This article briefly introduces three methods for the Controller to pass values to the view in Asp.net MVC, as well as differences between tempdata and viewdata. If you have other methods, please leave a message. Thank you.

Code:Http://cid-aef1e64945224a20.office.live.com/self.aspx/.Public/MvcApplication1.rar

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.