MVC4 notes @functions @model @using

Source: Internet
Author: User

/////@functions Custom Function//////

@helper Assist method can be very convenient to complete the development of the auxiliary method, but has lost some flexibility, for example, can not be customized in the @helper properties, only the simple pass-through parameters, and then format the image you want to direct output, So Razor also provides @functions custom function functionality that allows you to further define a more complex helper method in a way that is closer to the C # category.

You must first define a block of code through @functions, and then write the C # method in it, and if the method must return the execution results to the view page, you must do so with the ihtmlstring type. Usage instance. @functions {public ihtmlstring getyesterday () {var theday=datetime.now.adds ( -1); return new Htmlstring ( Theday.toshortdatestring ());}} In the same page as @functions, you can use the @getyesterday () method, same as the @helper helper class method, if you want to use this @functions defined method in several different view pages. You can move this @functions declaration to the cshtml file in the App_Code directory, but there is a small place to note, move past methods or properties that must be declared static to make each page available, it becomes @functions{public static ihtmlstring getyesterday () {var theday=datetime.now.adds ( -1); return new htmlstring (theday.toshortdatestring ());}} /////@model Reference parameter data type/////In the Razor page can be at the top of the page through the @model syntax to set a set of view pages of the strongly typed data Model reference, the data model number, in this view page can be used in a model five syntax declaration example: @model IEnumerable Note: The first letter of the @model syntax is lowercase m, which distinguishes it from the model entity. /////@using Reference namespace/////At the top of the Razor page, you can refer to the namespace used in this view page by @using to simplify the length of the program, taking the syntax of @model above as an example: @using Mvcapplication6.models@model IEnumerable in the Views\web.config profile, there is a < node system.web.webpage.razor> section settings, the bottom of a < node namespaces> The section sets the namespaces that are introduced for all view pages, and if most of the attempts are to introduce the same namespace, you can load them here without having to write on each page.

MVC4 notes @functions @model @using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.