First, Html.BeginForm <form> tags
View Code
@using (Html.BeginForm ("Search", "Home", Formmethod.get), new {target= "_black", @class = "Form1"}) { <input type= "Text" value= ""/>}
Generated HTML code action= "class="method= "target="_black ">
Type= "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 <input 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 <textarea 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 <label for= "Label1" > Hello </label>
V. Html.dropdownlist only allow Radio <select>
View Code
@{ list<selectlistitem> List = new List<selectlistitem> { new SelectListItem {Text = "Enabled", Value = "0 ", Selected = true}, new SelectListItem {Text =" disabled ", Value =" 1 "} }; @Html. DropDownList ("state", list,null,new{})
Generated HTML code
<select id= "state" name= "state" >
<option selected= "selected" value= "0" > Enable </option>
<option value= "1" > Disable </option>
</select>
Vi. Html.listbox allows multiple selection of <select>
View Code @{ list<selectlistitem> List = new List<selectlistitem> { new SelectListItem {Text = "enabled", Va Lue = "0", Selected = true}, new SelectListItem {Text = "disabled", Value = "1"} };} @Html. ListBox ("state", list)//generated The HTML code is <select id= "state" multiple= "multiple" name= "state" > <option selected= "selected" value= "0" > Enable </option> <option value= "1" > Disable </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 <input id= "Hidden1" name= "Hidden1" type= "Hidden" Value= "I am a hidden field"/>;
Viii. html.password <input type= "Password"/>
View Code @html.password ("Password1", 123321, new {@class = "class1"})//generated Html code for <input class= "Class1" id= "Password1" Name= "Password1" type= "password" value= "123321"/>
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 <input id= "Radio1" name= "Radio1" type= "Radio" value= "1"/><input id= "Radio1" name= "Radio1" type= " Radio "value=" 2 "/><input checked=" checked "id=" Radio1 "name=" Radio1 "type=" 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 {});//generated HTML code: Man: <input checked=" Checked "id=" Check1 "name=" Check1 "type=" checkbox "Value=" true "/& Gt;<input name= "Check1" type= "hidden" value= "false"/>; woman: <input id= "Check1" name= "Check1" type= "checkbox" Value= "true"/><input name= "Check1" type= "hidden" value= "false"/>; others: <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 <a 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 (); }
Inside the view there is a
@Html. TextBox ("Name");
Generated in the browser
Id= "name="type= "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 <input 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 tab of HTML helper method