Asp.net Mvc Framework 8 (Helper)

Source: Internet
Author: User
Tags actionlink
I am already crazy... I have been writing this article twice, so I cherish it

This section describes Helper in Asp. netMVC.
Helper is actually a method group written in View to implement some flexible functions.

In fact, the View of Asp.net MVC is An Aspx page, and the definition method can be declared. Why is there a Helper?
In fact, it is nothing more than separating the interface and logic, and Asp.net MVC does not only support Aspx View. In the extension package, there are also Castle's NVelocity engine and Boo, if a method is defined in Aspx, its scalability and portability will be affected. and the code is not very nice.

UrlHelperThe Action method is used to generate a hyperlink. Its overload is Public string Action (string actionName );
Public string Action (string actionName, object values );
Public string Action (string actionName, RouteValueDictionary valuesDictionary );
Public string Action (string actionName, string controllerName );
Public string Action (string actionName, string controllerName, object values );
Public string Action (string actionName, string controllerName, RouteValueDictionary valuesDictionary );

For example, if I write Url. Action ("Index", "Home") in the View, the address/Home/Index will be generated after the operation.
If the URL Routing rules in your system are always changing, This Helper is a required choice. Public string Encode (string url );

This is also a method for using UrlHelper. For example, the <% = Url. Encode ("Chinese") %> function is the same as Server. UrlEncode.
If you have special requirements, you can use the new 3.0 feature and extended method to add new features for UrlHelper.

HtmlHelper is another common Helper
It is used to generate HTML code.
Eg. <% = Html. ActionLink ("Homepage", "index", "Home") %>

<A href = "/Home/Index"> homepage </a> is generated.
Overload methods include: Public string ActionLink (string linkText, string actionName );
Public string ActionLink (string linkText, string actionName, object values );
Public string ActionLink (string linkText, string actionName, RouteValueDictionary valuesDictionary );
Public string ActionLink (string linkText, string actionName, string controllerName );
Public string ActionLink (string linkText, string actionName, string controllerName, object values); public string ActionLink (string linkText, string actionName, string controllerName, RouteValueDictionary valuesDictionary );

Of course, HTMLHelper has more types than UrlHelper.
For example, a Button <% = Html. Button ("name", "value", "onclick") %>

Generate <Button onclick = "onclick" name = "name" id = "name"> value </button>

CheckBox: <% = Html. CheckBox ("name", "text") %>

Generate <Input value = "text" name = "name" type = "checkbox"> & nbsp; text

Form: <% = Html. Form ("Home", "Index", formediahod. Post) %>

Generate <Form action = "/Home" method = "post"> System. Web. Mvc. SimpleForm

</Form>

Of course, there are also SubmitButton and Image methods. I will not talk about them here.

Note that the Html. Mailto method in Preview2 has some bugs. Avoid using this method whenever possible.

I was born with nothing, and I knew nothing.

Appendix:
There are still a few more articles about the function, so I want to discuss them. The following can be an example or an extension of Asp.net MVC. I don't know what you want to see, if you are interested, please reply and I will make a reference.

Asp.net Mvc Framework Series

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.