About @html label label, editor using ASP.

Source: Internet
Author: User

@Html Helper is a simple description of the basic HTML with the corresponding @html helper, @Html basically contains the HTML of the form control and common HTML
In @html, the with for is primarily for strongly typed HTML types.
Used to describe tags in @html, define student objects for auxiliary instructions,

[CSharp]View PlainCopy
  1. Public class Student
  2. {
  3. // <summary>
  4. /// name
  5. // </summary>
  6. ///   
  7. [DisplayName ("name")]
  8. public string Name
  9. {
  10. get;
  11. set;
  12. }
  13. // <summary>
  14. // Age
  15. // </summary>
  16. public int Age
  17. {
  18. get;
  19. set;
  20. }
  21. }



Define @model Student on the cshtml page
Label label, implemented in the Labelextensions class
1, @Html. Label ()
Returns an HTML label element and the property name of the property represented by the specified expression.
Parameter: string expression,string labelText
Expressions: An expression that represents the property to display
LabelText: Display text
Cases:

[HTML]View PlainCopy
    1. @Html. Label ("Weight") Output:<label for="Weight">weight</label>
    2. Abel ("name", "name") Output:<label for="name"> Name </label>


2, @Html. Labelfor ()
Similar to @html.label (), but mainly for strongly typed
Cases:

[HTML]View PlainCopy
  1. @Html. Labelfor (model=>mode. Name) Output: <label for="Name">name</label>
  2. @Html. Labelfor (model=>mode. Name, "name") Output:<label for="name"> Name </label>
  3. If the [DisplayName ("name")] attribute (reference System.ComponentModel;) is added to the entity above, it will be displayed:
  4. <label for="name"> Name </label> for displaying Chinese characters is convenient


3, @Html. Labelformodel ()
Example: @Html. Labelformodel ("name") Output: <label for= "" >name</label>

The editor tag, which represents the input form control in the application, is implemented in the Editorextensions
1, @Html. Editor ()
Returns an INPUT element that corresponds to each property in an object represented by an expression
Cases:

[HTML]View PlainCopy
    1. A, @Html. Editor ("name") output;<input class="Text-box single-line" id="name" name=" Name " type=" text " value=" " />


b, at load time for input initialization value, here with ASP. NET MVC new ViewBag attribute;

[HTML]View PlainCopy
  1. @{
  2. viewbag.namevalue="Zhang San";//Namevalue for dynamic type
  3. Or
  4. viewdata["Namevalue"]= "Zhang San";
  5. }
  6. @Html. Editor ("Namevalue") output <input class="Text-box single-line" id=" namevalue" name= "Namevalue" type="text" value="Zhang San" />


C, @Html. Editor ("name", Model.name)
The second parameter is the object Additionalviewdata parameter primarily for the view model of the data (model for the System.Web.Mvc.Model object, on the load page referencing the @model Student object),
But I do not know why input does not show the default value. Do not know is not not in ASP. NET MVC3 is not supported, want to know to give instructions under.
2, @Html. Editorfor ()
Returns an INPUT element that corresponds to each property in an object represented by an expression, mainly for strongly typed, which is generally used more often
A, @Html. Editorfor (Mode=>mode. Name)
If the returned view assigns a value to the student entity, the output output <input class= "Text-box single-line" id= "name" name= "name" type= "text" value= "Default value"/ > Otherwise value is "";
B, @Html. Editorfor (Mode=>mode. Name, "templatename")
The second parameter is the template name, which defines the template:
First create a folder in directory view/shared/editortemplates/templatename.cshtml that is: view/shared/editortemplates/templatename.cshtml
Note: It is important to use EditorTemplates as a folder
The templatename.cshtml code is: @Html. DropDownList ("", New SelectList (New []{"1", "2", "3"}) represents a drop-down list with a value of
You can then call @html.editorfor (Mode=>mode. Name, "templatename"), is displayed as a drop-down list, which appears as a text box if a text box is set in the template.
Also, the name attribute in the entity student can be added with the [UIHint ("templatename")] attribute if you add this, you can not display the specified template name at the time of the call
With @html.editorfor (Mode=>mode. Name) can be displayed as a drop-down box, and all of this field will become a drop-down list
Equivalent to the input control in HTML
@Html. Editorfor (Model=>model. Age)
The page is displayed as: <input id= "Age" name= "age" type= "text" value= ""/>
3, @Html. Editorformodel ()
If you use the default value without any parameters, the loop displays all the information in the model, but it needs to be placed in a loop
such as: <input class= "Text-box single-line" id= "name" name= "name" type= "text" value= ""/>
<input class= "Text-box single-line" id= "Age" name= "age" type= "text" value= ""/>

About @html label label, editor using ASP.

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.