Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 10:56:56 10月01日 Update

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!

Tags: Code mode data form content effects methods learning practicality deeper interest practicality practicality controller simple operation intelligence more friends articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info MySQL Shulou Technology Redmi