1024programmer Asp.Net ASP.NET Core MVC from entry to mastery (2)

ASP.NET Core MVC from entry to mastery (2)

ASP.NET Core MVC from entry to mastery (2)

With the development of technology, ASP.NET Core MVC has been launched for a long time. After continuous version updates and iterations, it has become more and more perfect. This series of articles mainly explains the process involved in developing B/S systems with ASP.NET Core MVC. The relevant content is suitable for beginners, school graduates, or other people who want to engage in ASP.NET Core MVC system development. After the explanations in the first two articles, I have a preliminary understanding of ASP.NET Core MVC project creation, startup and operation, as well as the naming convention of ASP.NET Core MVC, creating controllers, views, models, receiving parameters, etc. Today I will continue to explain ASP.NET Core MVC data is passed to the client and other related content, for learning and sharing purposes only.

With the development of technology, ASP.NET Core MVC has been launched for a long time. After continuous version updates and iterations, it has become more and more perfect. This series of articles mainly explains the process of developing B/S system with ASP.NET Core MVC. The relevant content involved is suitable for beginners, school graduates, or other people who want to engage in ASP.NET Core MVC system development. After the explanations in the first two articles, I have a preliminary understanding of ASP.NET Core MVC project creation, startup and operation, as well as the naming convention of ASP.NET Core MVC, creating controllers, views, models, receiving parameters, etc. Today I will continue to explain ASP.NET Core MVC data is passed to the client and other related content, for learning and sharing purposes only.

Overview

In the ASP.NET Core MVC project, the controller passes data to the view in the following common ways:

  1. ViewData is a collection of Key/Value pairs. Data objects can be stored and obtained conveniently through ViewData. However, the object type of the object obtained by ViewData mainly performs certain type conversions.
  2. ViewBag, a certain dynamic type object, needs to be parsed at runtime.
  3. Model model, for strongly typed views, can not only receive parameters through the model, but also return data.
  4. TempData, supports page jump data transfer. But it only supports one page jump data transfer.

ViewData

ViewData is an attribute of ViewDataDictionary type in the controller, which is used to store the dictionary collection of Key/Value and can be used directly in the controller.

ViewData characteristics

ViewData has the following characteristics:

  • ViewData is a Dictionary object inherited from the ViewDataDictionary class, used to transfer values ​​from the Controller to the corresponding View.
  • ViewData is only valid in the current request, its life cycle is the same as View, and its value cannot be shared among multiple requests.
  • After redirection, the variable value stored in ViewData will become null.
  • When retrieving the variable value in ViewData, appropriate type conversion (implicit or explicit) and null value checking must be performed.

ViewData example

To pass value through ViewData, first assign a value to ViewData in the controller, as shown below:

 1 /// 
  2 ///   ViewData
  3 ///   
  4 ///  
  5 publicIActionResult Test()
  6 {
  7 ViewData.Add("Name"  , "Mr.  Six");
  8 ViewData.Add("AgeName", "Gongzi Xiaoliu 1");
  4 TempData.Add("Name"  , "Mr.  Six 2")  ;
  5 return  span> View();
  6 }
  7
  8 publicIActionResult Test4()
  9 {
 10
 11 return View();
 12 }

The content of the view page is also very simple, as shown below:

The content of the Test3.cshtml page is as follows:

1 <a href="~/Hello/Test4">  Jump to Test4</a>

The content of the Test4.cshtml page is as follows:

 1 <h1>Welcome  Young Master Xiaoliu</h1>
  2
  3 <div>
  4 <span>NameViewData:span>
  5 <span>@ViewData["  Name"]</span>
  6 </div>
  7 <div>
  8 <span>Name TempData: span>
  9 <span>@TempData["  Name"]</span>
 10 </div>

Run the program and enter the URL [https://localhost:7116/Hello/Test3] in the browser, as shown below:

As can be seen from the above examples:

  1. After ViewData is passed on the page, the key values ​​added by the previous Action have been cleared.
  2. The value in TempData is still saved after the page jumps.
  3. TempData will be cleared after the second request and cannot be obtained after the third request.

Note: TempData only supports one cross-Action jump and does not support multiple jumps.

The above is the entire content of the second part of ASP.NET Core MVC from entry to mastery. It is intended to attract people, learn together, and make progress together.

Author: Xiaoliu Gongzi

Source: http://www.cnblogs.com/hsiang/

The copyright of this article belongs to the author and the blog park. It is not easy to write an article. Originality is supported. Reprinting is welcome [like it]. Please keep this statement when reprinting, and provide a link to the original text in an obvious position on the article page. Thank you.

Follow personal public accounts and regularly update technology and workplace articles

/span>

Run the program and enter the URL [https://localhost:7116/Hello/Test3] in the browser, as shown below:

As can be seen from the above examples:

  1. After ViewData is passed on the page, the key values ​​added by the previous Action have been cleared.
  2. The value in TempData is still saved after the page jumps.
  3. TempData will be cleared after the second request and cannot be obtained after the third request.

Note: TempData only supports one cross-Action jump and does not support multiple jumps.

The above is the entire content of the second part of ASP.NET Core MVC from entry to mastery. It is intended to attract people, learn together, and make progress together.

Author: Xiaoliu Gongzi

Source: http://www.cnblogs.com/hsiang/

The copyright of this article belongs to the author and the blog park. It is not easy to write an article. Originality is supported. Reprinting is welcome [like it]. Please keep this statement when reprinting, and provide a link to the original text in an obvious position on the article page. Thank you.

Follow personal public accounts and regularly update technology and workplace articles

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/asp-net-core-mvc-from-entry-to-mastery-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索