HTML helper Methods

Source: Internet
Author: User
Tags actionlink

I did not notice the details before using the HTML helper method in the process:

1. Html.BeginForm:

Used to submit the form, using the routing engine to find the submission path. Compared to <form>, when an application is deployed to a non-Web root directory or modifies a route definition, resources are not found.

2. Html.validationsummary:

An unordered list to display all validation errors in the Modelstate dictionary, generating <ul> displaying error messages. Use the Boolean type parameter to tell the accessibility method how the error level is displayed.

True displays only errors in modelstate that are related to the model itself. False is a property-related error in addition to displaying the model error.

3. Html.label:

The value of its existence in addition to the display of additional information, where the for attribute, typically contains the ID of the relevant INPUT element, if the user clicks the label then the browser will focus on the relevant input control. Used in check boxes and radio boxes.

4. Html.dropdownlist and Html.listbox (support multiple items need to set the multiple feature to multiple)

The background is built with SelectList or multiselectlist. The parameters for the SelectList constructor need to specify the original collection, the property name (Id) to use as the background value, the name to use as the display text, and the currently selected item.

5. Html.editor (html.editorfor) Template helper method, the default and html.textbox the same effect, the difference is that you can in the background by setting the [DataType] attribute above the property to generate an appropriate editor, such as [DataType ( Datatype.multilinetext)] Generates multiple lines of text.

When using Viewdata,viewbag, you should be aware of the problem with model binding:

    • For viewdata["Name"] and the view.name effect, you can think of two different types of access to the same value
    • When setting viewdata["name"]= "xxx", the foreground can get the value with Html.textbox ("name") or Html.textbox ("xxx", viewdata["name"]).
    • For Modelstate, the value is also saved in a property in its lookup table when the model binding fails. So that the view is rendered when it is used for its original error input form.
    • The helper method looks at the contents of the ViewData and can also see the object properties. As follows:
 PublicActionResult Edit (intID) {viewbag.student=NewStudent () {name="XXX"}; returnView ();}//Access in view@Html. TextBox ("Student.name")//Rendering Results<input id="Student_name"Name="Student.name"Type="text"Value="XXX"/>

Notice that the resulting INPUT element ID attribute uses an underscore instead of a point because the id attribute is illegal and the ID feature is typically used by client script. Another point to set the style in front of the class need to add the @ symbol

C # Property names with hyphens are not valid and need to be underlined instead, and all HTML helper methods convert the attribute name underline to hyphens when rendering. The following example (red marked):

Html.BeginForm ("","", Formmethod.get,new {@class=""data_name= " XXX "})// render effect <form action=" "class=" ", method= " Get " data-name= "XXX">

With strongly typed binding, for Html.textbox ("name") This form of helper, when viewdata["name"] and strong type exist simultaneously, how the Name field in the model is null or an empty string is used viewdata[" The value in Name "].

From this you can see that the helper method first looks at ViewData or viewbag before it finds the strongly typed model object.

You can also use a strongly-typed helper method that, in addition to replacing strings with a for suffix and a lambda expression, renders the same effect as before, but does not cause conflicts with keys in ViewData.

Rendering helper Methods

    • Html.ActionLink and Html.routelink: use simple both follow the same pattern, but RouteLink can only receive the route name, not the controller and operation name. There are a lot of different overloaded methods

Html.ActionLink ("Text display", "Action name", "Controller name", new {id = +}, NULL)

Html.routelink ("Text display", New {action= "action name", ...})

    • URL helper method: Action,content,routeurl
    • html.partial and html.renderpartial: You do not need to render the view directly through an operation in the controller, and you need to have the same model as the master view

Html.partial: Rendering a partial view into a string

RenderPartial: Write directly to the response stream, which needs to be written in the @{} code block. Has better performance

    • Html.action and Html.renderaction

Similar to the above, but these two perform separate controller operations, can build different models and leverage separate controller contexts for increased flexibility and reusability.

Typically used in conjunction with the Childactiononlyattribute feature, which effectively avoids the runtime invoking an action action on a controller directly through a URL

HTML helper Methods

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.