Use Htmlhelper to create a TextBox,

Source: Internet
Author: User

Use Htmlhelper to create a TextBox,

The following uses the HtmlHelper help class to create a text box.

Create an object class as follows:

Using System; using System. collections. generic; using System. linq; using System. web; namespace MVCRestartlearnning. models {public class Student {// <summary> // Student ID /// </summary> public int StudentId {get; set ;} /// <summary> /// name /// </summary> public string StudentName {get; set ;} /// <summary> /// Age /// </summary> public int Age {get; set ;} /// <summary> /// whether or not to enter the new school /// </summary> public bool isNewlyEnrolled {get; set ;} /// <summary> /// Password /// </summary> public string Password {get; set ;}}}
TextBox ();

The Html. TextBox () method creates the text box <input type = "text"/> and can contain the name, value, and html attributes;

TextBox method signature:

MvcHtmlString Html. TextBox (string name, string value, object htmlAttributes)

TextBox method has override overloads. Please visit MSDN to know all the overloads of TextBox () method.

The TextBox () method is a loosely typed method because name parameter is a string. the name parameter can be a property name of model object. it binds specified property with textbox. so it automatically displays a value of the model property in a textbox and visa-versa.

Example:

@ Html. TextBox ("student", null, new {@ class = "Myclass "})

Generation:

 <input class="Myclass" id="student" name="student" type="text" value="" />

In the preceding example, the first parameter "student" is set to the value of the id attribute and name attribute of the text box. The first parameter is used to display the value in the text box, the third parameter is used to set the class attribute of the text box. HtmlAttrbutes is an object type and an anonymous object. The attribute names start with a @ symbol;

In the above example, you can change the name of the first parameter without "student". However, it is not bound to the model;

  @Html.TextBox("myTextBox", "hello,TextBox", new { @class="myclasses"})

Generation:

Code:

<input class="myclasses" id="myTextBox" name="myTextBox" type="text" value="hello,TextBox" />
TextBoxFor ();

TextBoxFor is a strongly typed extension method that uses lambda expressions to generate text boxes for models;

The TextBoxFor method binds a specific model property to the text box, so the property value is automatically displayed in the text box;

Signature:

MvcHtmlString TextBoxFor(Expression<Func<TModel,TValue>> expression, object htmlAttributes)

Visit MSDN to know all the overloads of TextBoxFor () method.

 

Example:

@ Html. TextBoxFor (m => m. StudentName, new {@ class = "form-control "})

 

Generation:

 <input class="form-control" id="StudentName" name="StudentName" type="text" value="" />

:

 

In the preceding example, the first parameter of TextBoxFor is a lambda expression that specifies the StudentName attribute and binds it to the text box to generate the value of the id and name attributes with its name, if the value of StudentName is Tom, Tom is displayed in the text box;

 

Difference between TextBox and TextBoxFor:
  • @ Html. TextBox () is loosely typed method whereas @ Html. TextBoxFor () is a stronugly typed (generic) extension method.
  • TextBox is loose, while TextBoxFor is a strongly typed extension method;
  • TextBox () requires property name as string parameter where as TextBoxFor () requires lambda expression as a parameter.
  • TextBox requires the property name as a string type parameter, but TextBoxFor requires a lambda expression as a parameter;
  • TextBox doesn't give you compile time error if you have specified wrong property name. It will throw run time exception.
  • If you specify an incorrect attribute name, TextBox will not report a compilation error, but will report a running error during running;
  • TextBoxFor is generic method so it will give you compile time error if you have specified wrong property name or property name changes. (Provided view is not compile at run time .)
  • TextBoxFor is a generic method. It will give you a compilation error. If the attribute name you specified is incorrect, or the attribute name has changed. (The provided view will not be compiled during running)

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.