Asp.net Mvc Framework 11 (use of custom Helper in MVC)

Source: Internet
Author: User
Helper in Monorail is bound to the Controller.
Shape: [Helper (typeof (ChHelper)]
Abstract public class BaseBlockController: SmartDispatcherController {
}

However, Helper is used in View, so Monorail is slightly different from the separation method.

So how does Asp.net MVC bind a custom Helper?

In fact, the View of Asp.net MVC also has code files similar to index. aspx. cs.
Code files inherit from ViewPage

The following example is used:
First, I will customize a Helper
Small extensions of strings are implemented. Public class StringHelper
{
Public string Red (string str ){
Return string. Format (@ "<span style =" "color: red" ">{0} </span>", str );
}
Public string Green (string str ){
Return string. Format (@ "<span style =" "color: green" ">{0} </span>", str );
}
}

So how can we use it in View?
We can write the following code in View cs: Public StringHelper Str {
Get;
Set;
}

That is Public partial class Index: ViewPage
{
Public StringHelper Str {
Get;
Set;
}
}


In this way, we can use <% = Str. Red (ViewData ["title"]) %>

Such code

If you want to use Helper not only in this file
You can write the attribute in the base class of a BaseViewPage, and then all views are inherited from the custom BaseViewPage.

Similarly, you can use a similar method to use the custom Helper in the Master file.

Related Article

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.