The text box is created below through the HtmlHelper help class.
First create a new entity class, as the following example:
Using system;using system.collections.generic;using system.linq;using system.web;namespace mvcrestartlearnning.models{public class Student {//<summary>//// </summary> public int Stud Entid {get; set;}///<summary>//Name///</summary> public string Studentname {get; set;}//&L t;summary>///</summary> public int ages {get; set;}///<summary>//whether new admission//</summary& Gt public bool isnewlyenrolled {get; set;}///<summary>//password///</summary> public string Password {ge T Set; }}}
TextBox ();
Html.textbox () method, create text box "<input type=" text "/>", and can take name,value and HTML attributes;
Signature of the TextBox method:
Mvchtmlstring Html.textbox (string name, string value, Object htmlattributes)
TextBox method has many overloads. 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 is 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: Example:
@Html. TextBox ("student", NULL, new {@class = "Myclass"})
Generated:
<input class= "Myclass" id= "student" name= "student" type= "text" value= ""/>
In the above example, the first parameter, "Student", is set to the ID property of the text box and the value of the Name property, the first parameter is the value to display in the text box, the third parameter, which is used to set the class property of the text box, Htmlattrbutes is an object type, It is an anonymous object, and the property name begins with an @ symbol;
In the above example, the first argument, you can also change the name, do not "student", but will not be bound to the model;
@Html. TextBox ("MyTextBox", "Hello,textbox", new {@class = "myclasses"})
Generated:
Behind code:
<input class= "myclasses" id= "MyTextBox" name= "MyTextBox" type= "text" value= "Hello,textbox"/>
Textboxfor ();
The Textboxfor method is a strongly-typed extension method that uses a lambda expression to generate a text box for the model;
The Textboxfor method binds a specific model property to the text box, so the value of the property 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"})
Generated:
<input class= "Form-control" id= "Studentname" name= "Studentname" type= "text" value= ""/>
:
In the example above, the first parameter of textboxfor is a lambda expression that specifies the Studentname property and binds it to the text box, generating the value of the ID and Name property with its name, if the Studentname property value is Tom, then Tom is displayed in the text box;
Difference between TextBox and textboxfor:
- @Html. TextBox () is loosely typed method whereas @Html. Textboxfor () is a strongly typed (generic) extension method.
- The textbox is loosely typed, and textboxfor is a strongly typed extension method;
- TextBox () requires property name as String parameter where as textboxfor () requires lambda expression as a parameter.
- A TextBox requires a property name as a parameter of type string, whereas textboxfor requires a lambda expression as a parameter;
- TextBox doesn ' t give you compile the time error if you had specified wrong property name. It'll throw run time exception.
- If you specify an incorrect property name, the TextBox will not report a compile error, but will run the error when it runs;
- Textboxfor was generic method so it would give you compile time error if you had specified wrong property name or property Name changes. (provided view isn't compile at run time.)
- Textboxfor is a generic method that will give you a compile error if the property name you specified is wrong, or the name of the property has changed. (The provided view is not compiled at run time)
Use HtmlHelper to create a text box textbox