Summary of ASP. NET MVC helper and custom functions @functions

Source: Internet
Author: User

The ASP. Razor view has a. cshtml suffix that makes it easy to switch between C # code and HTML tags, greatly improving our development efficiency. But Razor grammar still has some cotton candy to be worth our understanding, can enhance our development efficiency more strongly, reduce the appearance of development bug.

Razor uses the @ tail symbol, which is the symbol that has made the MVC development more efficient. Here's a look at two reusable helper and functions related to @.

As a modern programmer, we adhere to a principle as much as possible. Don't repeat yourself. So we can refactor the code to merge, but this is for the background code C #, for the view layer can also carry out some simple business logic, of course, the view layer can be complex business logic judgment, but the predecessor said complex business logic is model or controller work, The task of the view layer is to show that the business logic should be as few as possible.

What are some of the refactoring in the view layer? One of them is the @helper custom fragment.

For example, if we want to output a number, if it is 0, then the output does not exist, if the output exists for other numbers, of course, this can be easily done under the powerful razor syntax.

1 @ (viewbag.isenabled = = "0" ?) " does not exist " : " exists ")

But what if the current page has many of these same logical judgments? A smart programmer must know that you can't repeat your own principles, so we're going to refactor, but how do we refactor in the view layer? Using @helper can solve this problem.

1@helper Show (intcount)2 {3     if(Count = =0)4     {5 @: Presence6     }7     Else8     {9 @: Not presentTen     } One } A  -@ (viewbag.isenabled = =0?"does not exist":"exist") -@Show (0) @* Call helper*@

This allows us to adjust the output for multiple positions on the current page, and if you want to modify it, you can modify the part, not all of them.

And I'm going to say, what do I do with this helper on other pages? Of course, there is the option to add a view file under the App_Code folder (assuming uihelper.cshtml), copy the helper code, and then call it through @uihelper.show (0) on the view page that needs to be invoked. Because the files under the App_Code folder will eventually be compiled into classes.

Summary: Let's summarize how many implementations of the output are to be implemented according to different circumstances, and of course what I can think of is not complete.

    1. This helper method is called by the helper to make the global settings, so that all pages need to be judged.
    2. In the background code to judge, and then output to the foreground view.
    3. by Html.action () or html.partial (), of course, if the simple or the use of helper, complex can be used in this way.
    4. Implemented by custom function functions.

Custom Function @functions, the custom function uses C # syntax to implement code reuse, except that the function can output HTML tags to the page.

1 //Custom Function @functions2 @functions {3      PublicIhtmlstring Get (intcount)4     {5         stringresult ="";6         if(Count = =0)7         {8result ="does not exist";9         }Ten         Else One         { Aresult ="exist"; -         } -         return Newhtmlstring (result); the     } - } -  -  +@Get (0)//Call the Custom function

Note that the @functions corresponds to the Razor code snippet, you need to add {}, and functions is the normal C # method inside.

If you want to use this functions on multiple pages at the same time, you can port this method to App_Code, assuming the file name is uihelper.cshtml. And the inside method must be defined as static. This is a good understanding that Uihelper is equivalent to a class name, where functions is the equivalent of a method, and if it is to be called by the class name. Method name, the method must be defined as static.

uihelper.cshtml File Code

1@helper Showunit (intcount)2 {3     if(Count = =0)4     {5 @: Free6         }7     Else8     {9 @countTen     } One } A  - @functions { -      Public StaticIhtmlstring Check (intcount) the     { -         stringresult =""; -         if(Count = =0) -         { +result ="FSDFSDFSDFD"; -         } +         Else A         { atresult =count. ToString (); -         } -         return Newhtmlstring (result); -     } - } - //Custom Function @functions in @functions { -      Public StaticIhtmlstring Get (intcount) to     { +         stringresult =""; -         if(Count = =0) the         { *result ="does not exist"; $         }Panax Notoginseng         Else -         { theresult ="exist"; +         } A         return Newhtmlstring (result); the     } +}

Summary: Helper is for direct output HTML content with simple logic, and helper does not have any return value, and functions Custom function is much more powerful, if functions need to return HTML content, The return value is the ihtmlstring type and can be set to void if no return value is required, but if no return value is lost, the meaning of the function is defined, so the general return value is ihtmlstring. For the refactoring of view layer, we can use helper and custom function functions to implement.

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.