Summary of several forms of data transfer by MVC in ASP. NET

  • 2021-07-02 23:56:09
  • OfStack

This article illustrates several forms of MVC data transfer in ASP. NET. Share it for your reference. The details are as follows:

In Asp. net mvc development, Controller needs to provide Model to View, and then View renders this Model to HTML. This paper introduces three ways to transfer data from Controller to View, and realizes the display of one DropDownList.

Type 1: ViewData

ViewData is an Dictionary. It is very simple 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 in View to take the value:


<div class="editor-field">
    <%= Html.DropDownList("Country", ViewData["Countries"] as SelectList) %>
    <%: Html.ValidationMessageFor(model => model.Country) %>
</div>

The above code uses as to convert it to SelectList.

The code for handling POST is as follows:


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

Type 2: ViewModel

Using ViewModel, we first create an 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 using ViewModel to store data in 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 method is better than the first method in that it supports intelligent sensing.

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

Type 3: TempData

The methods of using TempData and using ViewData are identical.

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:


<div class="editor-field">
  <%= Html.DropDownList("Country", TempData["Countries"] as SelectList) %>
  <%: Html.ValidationMessageFor(model => model.Country) %>
</div>

Effect: The first way is one.

Differences 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 emptied, but TempData has not been emptied, which is the first difference between them.

I hope this article is helpful to everyone's asp. net programming.


Related articles: