First, Html.BeginForm <form> tags
//View Code
@using (Html.BeginForm ("Search", "Home", Formmethod.get), new {target= "_black", @class = "Form1"}) {<inputtype= "text"value="" />}
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 = "All" />
Third, Html.textarea <textarea> tags
//View Code @html.textarea ("Textarea1", "I am a TextArea", new {@class = "Text_style"})//generated Html code<textareaclass= "Text_style"cols= " the"ID= "Textarea1"name= "Textarea1"rows= "2">I'm 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> { new SelectListItem {Text = "Enabled", Value = "0", Selected = true}, new Selec Tlistitem {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 for @{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"> <optionselected= "Selected"value= "0">Enable</option> <optionvalue= "1">Disable</option></Select>
Vii. html.hidden <input type= "Hidden"/>
//View Code @html.hidden ("Hidden1", "I am a hidden field", new{});//Html code output to the browser < ID= "Hidden1" name= "Hidden1" type= "hidden" Value= "I am a hidden domain"/>;
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 = "123321" />
Nine, Html.radiobutton <input type= "Radio"/>
//View Code @html.radiobutton ("Radio1", 1,false) @Html. RadioButton ("Radio1", 2,false) @Html. RadioButton ("Radio1", 3, true)//generated HTML code for<inputID= "Radio1"name= "Radio1"type= "Radio"value= "1" /><inputID= "Radio1"name= "Radio1"type= "Radio"value= "2" /><inputchecked= "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:<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" />; others:<inputID= "Check1"name= "Check1"type= "checkbox"value= "true" /><inputname= "Check1"type= "hidden"value= "false" />;
Xi. ActionLink <a>
@Html. ActionLink ("list pages", "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.
PublicActionResult Index () {viewbag.name="Zhang San"; returnView (); }
Inside the view there is a
@Html. TextBox ("Name");
Generated in the browser
<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:
//Backend Code public class mans {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 tab of HTML helper method