ASP.NET Core MVC from entry to mastery (1)
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.
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 explanation in the previous article, 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, etc. Today I will continue to explain ASP.NET Core MVC Parameter reception, transmission and other related content are for learning and sharing only.
Model model
In the ASP.NET Core MVC project, the model describes the data that needs to be rendered on the user interface, or is a part of this data. Here we need to distinguish the concepts of entity and model. Entity is the data used in business logic. The structure is generally consistent with the corresponding table in the database; the model is the data that the page can receive after conversion. For example: sensitive information such as ID in the database, bool type, date type conversion, etc. are not suitable for directly binding Entity entities to the page and need to be converted into a model for display.
Create model
In the ASP.NET Core MVC project, the model is an ordinary class. On the Models folder, right-click [Add] [Class (C)…] to open the Add Class dialog box, as shown below:
In the Add Class dialog box, enter the name Student and click OK, as shown below:
Note: In the ASP.NET Core MVC project, there is no convention on the model name, just conform to the class naming convention. Except the identifier must start with a letter or underscore (_
). It is recommended that the naming follow the following rules:
- [Rule 1-1] Use Pascal rules to name class names, that is, the first letter must be capitalized.
- [Rule 1-2] Name the class using a noun or noun phrase that reflects the function of the class.
- [Rule 1-3] Do not use prefixes with specific meanings such as “I”, “C”, and “_”.
- [Rule 1-4] Custom exception classes should end with Exception.
- [Rule 1-5] The file name should reflect the content of the class, and it is best to have the same name as the class.
After the model Student is successfully created, add relevant attributes as follows:
1 namespace DemoCoreMVC.Models 2 { 3 ///4 /// Student model 5 /// 6 public class Student 7 { 8 /// 9 /// unique identifier 10 /// 11public int Id { get; set; } 12 13 /// 14 /// Student name 15 /// 16 public string Name { get; set; } 17 18 /// 19 /// Student age 20 /// 21 public int Age { get; set; } 22 23 /// 24 /// Student gender 25 /// 26 public string Sex { get; set; } 27 } 28 }
View binding model
First create an empty view model [you can also create a strongly typed model view], and specify the model for the view through @model DemoCoreMVC.Models.Student. You can then use the model for data binding, as shown below:
1 @model DemoCoreMVC. Models.Student 2 @{ 3 4 } 5 < span>h1> Welcome Mr. Xiaoliu</h1> 6 <div> 7 <span>Student ID: span> 8 <span>@Model.Id </span> 9 </div> 10 <div> 11 <span>Name: span> 12 <span>@Model.Name </span> 13 </div> 14 <div> 15 <span>Age: span> 16 <span>@Model.Age </span> 17 </div> 18 <div> 19 <span>Gender: span> 20 <span>@Model.Sex </span> 21 </div>
Controller delivery model
Initialize the model data in the controller, and then pass the created model data to the view through the View(model) method, as shown below:
1 using DemoCoreMVC.Models; 2 using span> Microsoft.AspNetCore.Mvc; 3 4 namespace DemoCoreMVC.Controllers 5 { 6 public span> "Id"]; 6 var span> name = Request.Form["Name"] ; 7 var span> age = Request.Form["Age"] ; 8 var span> sex = Request.Form["Sex"] ; 9 var span> student = new Student() 10 { 11 Id = string .IsNullOrEmpty(id) ? 0 : int.Parse(id), 12 Name = name , 13 Age = string .IsNullOrEmpty(age) ? 0 : int.Parse(age), 14 Sex = sex 15 }; 16 return span> Json(student); 17 }
Run the test and enter the URL [https://localhost:7116/Hello/add] in the browser to test, as shown below:
Receive parameters through the model
More often, for simplicity, we usually use a model to receive parameters. If the attribute name of the model is consistent with the key of the parameter, it can be automatically matched, which is very convenient, as shown below:
1 ///2 /// Receive parameters through the model 3 /// 4 /// 5 /// 6 public IActionResult ShowStudent4(Student student) 7 { 8 return span> Json(student); 9 } 10 11 12 [HttpPost] 13 public span> IActionResult Save2(Student student) 14 { 15 return span> Json(student); 16 }
Note: Regardless of the Get method or the Post method, the Model model can be used to receive parameters. The effect is the same as above and will not be demonstrated here.
The above is the first part of the integration and development of ASP.NET Core MVC from entry to proficiency. The second part of the integration and development will be further explained in the future. The purpose is to introduce new ideas, review the past and learn new things, 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
color: rgba(0, 0, 0, 1)”>IActionResult Save2(Student student)
14 {
15 return span> Json(student);
16 }
Note: Regardless of the Get method or the Post method, the Model model can be used to receive parameters. The effect is the same as above and will not be demonstrated here.
The above is the first part of the integration and development of ASP.NET Core MVC from entry to proficiency. The second part of the integration and development will be further explained in the future. The purpose is to introduce new ideas, review the past and learn new things, 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