Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the forms of data transferred by MVC in ASP.NET

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly talks about "what are the forms of data transmission by MVC in ASP.NET". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the forms of data transmission by MVC in ASP.NET?"

In Asp.net mvc development, Controller needs to provide Model to View, and then View renders the Model into HTML. This article introduces three ways to transfer data from Controller to View to achieve a DropDownList display.

The first kind: ViewData

ViewData is a Dictionary. It's very easy to use, look at the following code:

Public ActionResult ViewDataWay (int id) {Book book = bookRepository.GetBook (id); ViewData ["Countries"] = new SelectList (PhoneValidator.Countries, book.Country); return View (book);}

Use the following code to take a value in View:

Model.Country)% >

The above code uses as to convert it to SelectList.

The processing code for POST is as follows:

[HttpPost] public ActionResult ViewDataWay (int id, FormCollection collection) {Book book = bookRepository.GetBook (id); UpdateModel (book); bookRepository.Save (book); return RedirectToAction ("Details", new {id=id});}

The second kind: ViewModel

Using ViewModel, let's first create a BookViewModel with the following code:

Public class BookViewModel {public Book Book {get; set;} public SelectList Countries {get; set;} public BookViewModel (Book book) {Book = book; Countries = new SelectList (PhoneValidator.Countries,book.Country);}}

The code for storing data using ViewModel in the Aciton of the controller is as follows:

Public ActionResult ViewModelWay (int id) {Book book = bookRepository.GetBook (id); return View (new BookViewModel (book));}

In View, this approach is better than the first: it supports intelligent sensing.

The effect is the same as the first way.

The third kind: TempData

Using TempData is the same as using the ViewData method.

The Action code is as follows:

Public ActionResult TempDataWay (int id) {Book book = bookRepository.GetBook (id); TempData ["Countries"] = new SelectList (PhoneValidator.Countries, book.Country); return View (book);}

The code for the value of View is as follows:

Model.Country)% >

Effect: the first way is the same.

The difference between TempData and ViewData

Do a simple test to see the difference between TempData and ViewData.

Public ActionResult Test1 () {TempData ["text"] = "1-2-3"; ViewData ["text"] = "1-2-3"; return RedirectToAction ("Test2");} public ActionResult Test2 () {string text1 = TempData ["text"] as string; string text2 = ViewData ["text"] as string; return View ();}

After RedirectToAction jumps to Action, the value of ViewData has been cleared, while TempData has not been cleared, which is one of the differences between them.

At this point, I believe you have a deeper understanding of "what is the form of MVC data transmission in ASP.NET?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report