PreviousArticle(Learn ASP. NET 4.5 mvc4.0 (I) WITH ME) We have a basic understanding of some simple changes in ASP. NET mvc4.0, mainly for mvc3.0. These columns are mainly intended for beginners of ASP. net mvc, or ASP. NET developers who have not yet used MVC for training and learning. Of course, I can also learn about this development mode. Therefore, this article has to explain some razor syntax and some skills and development methods in ASP. net mvc development. Because I do not like Entity Framework during development, I need to explain the three-tier architecture and MVC development mode applications that I think. In the self-developed three-tier architecture, the database layer and logic layer remain unchanged. The only difference is that the UI Layer is changed to the MVC mode. Of course, if the model instance needs to be re-written, otherwise it will be replaced directly by the entity class. This is only because I personally know it may be incorrect. I hope you can give me more advice.
First, let's take a look at the razor syntax. In vs2010 or vs11, adding an attempt will make you choose to use the razor (cshtml) or C # (aspx) format. Razor's syntax beautification is obvious, but sometimes some inexplicable errors may occur, but it can generally be solved by adding HTML tags or "()". and so on. This may be a problem in the Text Template.
Use @ in razor to replace the encoding format of <%>.CodeBlock, you need to use "@ {}". You can write C # code in it. With vs, you can only parse it. Generally, anyone with development experience can easily get started. For more syntax-related questions, refer to the basic syntax of the mvc3 razor view engine. The following describes several important keywords in MVC, it is often used in programming.
I. @ model definition
The @ model keyword can be used to define a model (often called its object class) corresponding to an action. In fact, it is used to instantiate dynamic variables, in this way, you can directly call the "model" variable in the cshtml file. The model instance needs to be transmitted through the Controller. If not, the "model" is null.
The model can be an object class or a list instance, and dictionary objects can be defined. However, the model must be the same as the instance passed back by the action in the Controller. Otherwise, an error may occur. For example, we can obtain the user instance and present the user's specific information on the page, so that the user instance can be returned to the front-end cshtml page, the specific code is as follows.
1 /// <Summary>
2 /// Obtain user information.
3 /// </Summary>
4 /// <Returns> Returns the behavior result. </Returns>
5 Public Actionresult userinfo ()
6 {
7 VaR Id = convert. toint32 (request. querystring [
" ID " ] ???
" 0 " );
8 VaR User = users. getuser (ID );
9 Return View (User );
10 }
You only need to define "@ model user" on the cshtml page so that the basic model of this page (model) is the user class. Of course, if it is not empty, you can directly call it.
2. @ section node keywords
First, you need to understand the layout page in ASP. net MVC stipulates that files starting with "_" cannot be accessed on the browser, while the "shared" folder is a shared folder or a common folder. Generally, a sharedcontroller cannot be defined. I have never tried this. If you are interested, try it. The file specified in "_ viewstart. cshtml" is the layout page, which is equivalent to the master page in ASP. NET. It should be better understood. Each layout page must call a method "@ renderbody ()", and this @ section is a bit like placeholder. The well-known block name must be the same as the name defined in the layout page.
As you can see clearly above, the required parameter indicates whether the subpage must be rewritten. Generally, it is not required to be set to false. If it is true, all the word pages on the layout page must have a code block that is reproduced by this node. You can call the code in the subpage as shown in the following figure.
Because the @ section header sets required: false, it does not matter if it is not overwritten. Ignore it. Nodes generally use the "{}" package, but the following section does not specify the body section. Using a good layout page can reduce a lot of code, which is recommended.
3. @ helper method definition
You can use the @ helper keyword to define a method, so that you can call this method on the page, just like the method in C. The methods defined in the page can access the properties of viewbag, httpcontext, and other pages. The returned type is "helperresult ". There is no difference between other writing methods and common methods. It is mainly used for the list and so on. Repeated writing is required for repeated calls.
This keyword can also be used to define multiple method sets in the app_code folder. The most common method is to define paging. For example, you only need to input the number of records and the number of records displayed on each page, as well as the current page number and URL formatting parameters, you can easily present the page content on the page. We recommend that you define it in the app_code folder because the namespace can be omitted, and the file name is the class name, And the defined method can directly use "filename. methodname "format call, that is, static method class. Therefore, the methods defined here cannot use the viewbag and other attributes on the page.
Using the above three keywords not only gives a deep understanding of MVC, but also improves code reuse. Next I will introduce ASP. net MVC, the correspondence between the folder structure and MVC is a big problem for beginners, because I found that the developer just did not find the page, no controller is found on the page. The file display structure such as vs11 is better understood, as shown in.
It is easy to understand,MOdels-VIews-COntrollers forms MVC, which defines some model entities in models and adds some verifications. These model entity classes are called by methods in controller controllers. The type defined in controllers corresponds to the folder name in the Views folder. The name of the folder with "controller" is the name of the controller. This is a naming convention like attribute. The method is of course for the file names in the folder under views, which are all one-to-one. Of course, some controllers do not need to define these file names if they do not return an attempt. However, if the attempt is returned, the cshtml page cannot be found.
Note: The naming convention for controllers is only for Internet applications, but for webapis.
both the one I wrote yesterday and the one I said today are relatively basic. Learning Needs to be done step by step so that I can gradually get into the subject. I hope what I wrote can bring some gains to others. I did not plan the content of my writing as a whole. I often think about what to write, so it may bring a bad feeling to beginners. Please forgive me.