First, Html.BeginForm <form> tags
View Code @using (Html.BeginForm ("Search", "Home", Formmethod.get), new {target= "_black", @class = "Form1"}) {<Inputtype= "text" value< Span style= "color: #0000ff;" >= "" />}//Generated HTML code <form action= "/home/search" class= "form1" method= "get" target= "_black" > <input type=" text "value=" "/> </form>
New is called the Htmlattributes, can set the HTML property of this control, as for class preceded by a @ is because class in C # is a keyword.
Second, Html.textbox <input type= "text"/> Label
View Code @html.textbox ("Age", "23°c", new {@class = "Text1"})//generated Html code <class= "Text1" id= "age" name= "Age" type= "text" value/>
Third, Html.textarea <textarea> tags
View Code @html.textarea ("Textarea1", "I am a TextArea", new {@class = "Text_style"})//generated Html code <class= " Text_style " cols=" id = "textarea1" name= "TEXTAREA1" rows= "2"> I am a textarea</textarea>
Iv. Html.label <label> Labels
View Code @html.label ("Label1", "hello")//generated Html code <for= "Label1"> Hello </Label>
V. Html.dropdownlist only allow Radio <select>
View Code @{ list<selectlistitem> list = new list<selectlistitem>
Generated HTML code
<select id= "state" name= "state" > <option selected= "selected" value= "0" > Enable </option> <option V Alue= "1" > Disable </option> </select>
Vi. Html.listbox allows multiple selection of <select>
The View code is @{List<SelectListItem> list = new list<SelectListItem>{new SelectListItem {text = "Enabled", value = "0", Selected = true}, new SelectListItem {text = "disabled", value = "1"}}; } @Html. ListBox ("state", list)//generated Html code for<SelectId= "State"Multiple= "multiple"Name= "state" > << Span style= "color: #800000;" >option selected= "Selected" Value= "0" > enable </option> < option value= "1" > disables </option</select>
Vii. html.hidden <input type= "Hidden"/>
View Code @html.hidden ("Hidden1", "I am a hidden field", new{});//output to the browser Html code <id= "hidden1" name= "Hidden1 " Type=" hidden " value/>;
Viii. html.password <input type= "Password"/>
View Code @html.password ("Password1", 123321, new {@class = "class1"})//generated Html code for <class= "Class1" ID = "Password1" name= "Password1" type= "password" value/>
Nine, Html.radiobutton <input type= "Radio"/>
View Code @html.radiobutton ("Radio1", 1,false) @Html. RadioButton ("Radio1", 2,false) @Html. RadioButton ("Radio1", 3,true) The generated HTML code is<InputId= "Radio1"Name= "Radio1"Type= "Radio"Value= "1"/><input id= "Radio1" Span style= "color: #ff0000;" > Name= "Radio1" Type= " Radio " Value=" 2 "/> <input checked< Span style= "color: #0000ff;" >= "Checked" Id= "Radio1" Name=" Radio1 " type< Span style= "color: #0000ff;" >= "Radio" Value= "3" /
X. Html.checkbox <input type= "checkbox"/>
View Code man: @Html. CheckBox ("Check1", True, new {}), woman: @Html. CheckBox ("Check1", False, new {}), other: @Html. CheckBox ("Check1 ", False, new {});//The generated HTML code is: Man:<InputChecked= "Checked"Id= "Check1"Name= "Check1"Type= "checkbox"Value= "true"/><InputName= "Check1"Type= "hidden"Value= "false"/>Woman<InputId= "Check1"Name= "Check1"Type= "checkbox"Value= "true"/><InputName= "Check1"Type= "hidden"Value= "false" />; Other:<input id= "check1" name = "check1" type= "checkbox" value= "true" /><input name= "Check1" type= "hidden" value= " False " />;
Xi. ActionLink <a>
@Html. ActionLink ("List page", "lists")//generated Html code <href= "/home/list"> List page </a>
12. Automatic Binding
N, helper methods help bind to the control while building the UI
For example:
This is a controller.
Public ActionResult Index () { Viewbag.name = " Zhang San ; return View ();} In the view there is a @Html. TextBox ("Name"); Browser generated <input id= "name" name= "name" type= "text" value = " Zhang San"/>
We see that when we build the UI, we set up a viewbag.name, and the View has a textbox ("name"), and in the same name, MVC automatically binds us to the data. Let's look at one more:
Back-end code public class Man {public string Name {get; Set }} public ActionResult Index () {Viewbag.man = new Mans {Name = "Zhang San"}; return View (); }//View Code @html.textbox ("Man. Name ")//generated HTML code <id=" man_name " name=" man. " Name " Type=" text " value/>
Notice that the name of the ID in the. has become underlined, which is thought to be "." In the ID is not legal, but also to be left for JavaScript.
Form label for HTML helper method