When writing a page for editing data, we usually write the following code 1: <inputtype = "text" value = '<% = viewdata ["title"] %> 'name = "title"/> the Helper evolution we mentioned in the previous article, can we also use a helper to automatically bind data to this type of code? Of course, this is OK, Asp. net MVC provides htmlhelper to generate HTML tags with data bindings.
1. actionlink
The most common method is HTML. actionlink 1.1.
1: <% = html. actionlink ("this is a connection", "Index", "home") %> when the urlrouting rule is the default rule, the generated HTML code is 1: <ahref = "/"> This is a connection </A> the three parameters in actionlink are the displayed text action controller.
The controller can be omitted. If omitted, it points to the action under the same controller.
Querystring and HTML attribute settings in 1.2actionlink
1: Write method with querystring
2: <% = html. actionlink ("this is a connection", "Index", "home", new {page = 1}, null) %>
3: <% = html. actionlink ("this is a connection", "Index", new {page = 1}) %>
4: Other HTML attributes
5: <% = html. actionlink ("this is a connection", "Index", "home", new {id = "link1"}) %>
6: <% = html. actionlink ("this is a connection", "Index", null, new {id = "link1"}) %>
7: Both querystring and HTML attributes exist.
8: <% = html. actionlink ("this is a connection", "Index", "home", new {page = 1}, new {id = "link1"}) %>
9: <% = html. actionlink ("this is a connection", "Index", new {page = 1}, new {id = "link1"}) %>
The generated result is:
1: Write method with querystring
2: <ahref = "/? Page = 1 "> This is a connection </a>
3: <ahref = "/? Page = 1 "> This is a connection </a>
4: Other HTML attributes
5: <ahref = "/? Length = 4 "id =" link1 "> This is a connection </a>
6: <ahref = "/" id = "link1"> This is a connection. </a>
7: Both querystring and HTML attributes exist.
8: <ahref = "/? Page = 1 "id =" link1 "> This is a connection </a>
9: <ahref = "/? Page = 1 "id =" link1 "> This is a connection </a>. In this way, you can use actionlink to generate almost all address connections.
NOTE: If action and controller are not involved in the connection, there is no need to use actionlink. Instead, you can directly write HTML code. For example
1: <a href = "#1"> chapter </a>
2: <a href = "javascript: void (0)" onclick = "Delete ();"> Delete </a>
2. routelink
2.1 routelink and actionlink
Routelink is similar to actionlink, but its address is generated by route.
1: <% = html. actionlink ("this is a connection", "Index", "home") %>
If routelink is used for writing
1: <% = html. routelink ("this is a connection", new {controller = "home", Action = "Index"}) %>
Actionlink with querystring and HTML attributes
1: <% = html. actionlink ("this is a connection", "Index", new {page = 1}, new {id = "link1"}) %>
You can write it like this.
1: <% = html. routelink ("this is a connection", new {Action = "Index", page = 1}, new {id = "link1"}) %>
In fact, it is to replace the original action with a newly created routevaluedictionary object (the object instantiated by new {} will be converted to routevaluedictionary), which is separately specified by the Controller string.
2.2routelink use Route rules
In addition to these cooperative usage, routelink also supports using route rule names to create connections
For example, we add a route rule to the Global. asax file.
Routes. maproute ("about", // This is the rule name
"About", // URL
New {controller = "home", Action = "about "});
Then we can use this route rule
1: <% = html. routelink ("about", "about", new {}) %>
2: <% = html. routelink ("about", "about", new {page = 1}) %>
3: <% = html. routelink ("about", "about", new {page = 1}, new {id = "link1"}) %>
To generate the following HTML:
1: <ahref = "/about"> about </a>
2: <ahref = "/about? Page = 1 "> about </a>
3: <ahref = "/about? Page = 1 "id =" link1 "> about </a>
3. Form
In many cases, you need to generate a form element. As described in the article, when you modify a content, you may need to bind the data to the form.
3.1 generate form
Of course, we can use pure HTML code or urlhelper to generate a form. For example
<Form action = "/home/Index" method = "Post"> 2: </form>
<Form action = "<% = URL. Action (" Index "," home ") %>" method = "Post"> 2: </form>
But it is still difficult to maintain because it is in HTML attributes. Fortunately, ASP. net mvc provides us with a helper. we can generate a form in the following two ways:
1: <% using (HTML. beginform ("Index", "home", formmethod. post) {%> 2: Form Content 3: <%}%> 4: <% HTML. beginform ("Index", "home", formmethod. post); // note that no = output %> 5: Form Content 6: <% HTML. the endform (); %> beginform method is similar to the call method of actionlink,
Therefore, ASP. net mvc also provides the beginrouteform method. Of course, here we can also use new {} to add the querystring or HTML attribute for the form action. The method is similar to the previous one. See the method list.
3.2 form elements
ASP. net mvc provides helper for various form elements. The options include Textbox (similar to input type = text), textarea, dropdownlist (select), checkboxhidden, ListBox, password, and radionbutton. Note: Because <input type = "Submit"/> is generally not bound to data, ASP. net mvc does not provide this helper (previously provided before preview2 ). If you want to provide an input type = text whose name is T1, the following code: <% = html. Textbox ("T1") %>
3.3 form Element Binding
If we want to have a value at the beginning of T1, for example, "classic", we can do the following:
1: <% = html. Textbox ("T1", "classic") %>
If the data is read from the database, that is, the data is obtained from the action, we can use viewdata in the action to pass the action:
1: viewdata ["name"] = ""; view: 1: <% = html. Textbox ("T1", viewdata ["name"]) %>
The above method seems simple, in fact ASP. net MVC provides us with a simpler binding method-as long as the key of viewdata is consistent with the name of the element generated by helper, We can automatically bind: Action: 1: viewdata ["T1"] = "classic ";
View: 1: <% = html. Textbox ("T1") %> to automatically bind
3.4 List data display and binding
A single value such as textbox is relatively easy, but there are a lot of data dropdownlist or ListBox, how should we bind the data and the initialization value? Let's take a look at the following example:
Action: 1: viewdata ["sel1"] = new selectlist (new [] {1, 2, 3}, 3 );
View: 1: <% = html. dropdownlist ("sel1") %>
In this way, you can bind the list content, default value, and form elements together.
The contents of our list are not arrays in any case, but mostly key-value pairs.
You can use the following methods:
List <selectlistitem> List = new list <selectlistitem>
{
New selectlistitem {text = "", value = "1 "},
New selectlistitem {text = "ZOU Jian", value = "2 "},
};
Viewdata ["sel1"] = new selectlist (
List
, "2"
);
10. Textbox, hidden:
<% = Html. Textbox ("input1") %>
<% = Html. Textbox ("input2", model. categoryname, new {@ style = "width: 300px;"}) %>
<% = Html. Textbox ("input3", viewdata ["name"], new {@ style = "width: 300px;"}) %>
<% = Html. textboxfor (A => A. categoryname, new {@ style = "width: 300px;"}) %>
Result:
<Input id = "input1" name = "input1" type = "text" value = ""/>
<Input id = "input2" name = "input2" style = "width: 300px;" type = "text" value = "beverages"/>
<Input id = "input3" name = "input3" style = "width: 300px;" type = "text" value = ""/>
<Input id = "categoryname" name = "categoryname" style = "width: 300px;" type = "text" value = "beverages"/>
11. textarea
<% = Html. textarea ("input5", model. categoryname, 3, 9, null) %>
<% = Html. textareafor (A => A. categoryname, 3, 3, null) %>
Result:
<Textarea Cols = "9" id = "input5" name = "input5" rows = "3"> beverages </textarea>
<Textarea Cols = "3" id = "categoryname" name = "categoryname" rows = "3"> beverages </textarea>
12. checkbox
<% = Html. checkbox ("CHK1", true) %>
<% = Html. checkbox ("CHK1", new {@ class = "checkbox"}) %>
<% = Html. checkboxfor (A => A. isvaild, new {@ class = "checkbox"}) %>
Result:
<Input checked = "checked" id = "CHK1" name = "CHK1" type = "checkbox" value = "true"/>
<Input name = "CHK1" type = "hidden" value = "false"/>
<Input class = "checkbox" id = "CHK1" name = "CHK1" type = "checkbox" value = "true"/>
<Input name = "CHK1" type = "hidden" value = "false"/>
<Input checked = "checked" class = "checkbox" id = "isvaild" name = "isvaild" type = "checkbox" value = "true"/>
<Input name = "isvaild" type = "hidden" value = "false"/>
13. ListBox
<% = Html. ListBox ("lstbox1", (selectlist) viewdata ["categories"]) %>
<% = Html. listboxfor (A => A. categoryname, (selectlist) viewdata ["categories"]) %>
Result:
<Select id = "lstbox1" multiple = "multiple" name = "lstbox1">
<Option value = "1"> beverages </option>
<Option value = "2"> condiments </option>
<Option selected = "selected" value = "3"> confections </option>
<Option value = "4"> dairy products </option>
<Option value = "5"> grains/cereals </option>
<Option value = "6"> meat/poultry </option>
<Option value = "7"> produce </option>
<Option value = "8"> seafood </option>
</SELECT>
<Select id = "categoryname" multiple = "multiple" name = "categoryname">
<Option value = "1"> beverages </option>
<Option value = "2"> condiments </option>
<Option value = "3"> confections </option>
<Option value = "4"> dairy products </option>
<Option value = "5"> grains/cereals </option>
<Option value = "6"> meat/poultry </option>
<Option value = "7"> produce </option>
<Option value = "8"> seafood </option>
</SELECT>
14. dropdownlist
<% = Html. dropdownlist ("ddl1", (selectlist) viewdata ["categories"], "-- select one --") %>
<% = Html. dropdownlistfor (A =>. categoryname, (selectlist) viewdata ["categories"], "-- select one --", new {@ class = "dropdownlist"}) %>
Result:
<Select id = "ddl1" name = "ddl1">
<Option value = ""> -- select one -- </option>
<Option value = "1"> beverages </option>
<Option value = "2"> condiments </option>
<Option selected = "selected" value = "3"> confections </option>
<Option value = "4"> dairy products </option>
<Option value = "5"> grains/cereals </option>
<Option value = "6"> meat/poultry </option>
<Option value = "7"> produce </option>
<Option value = "8"> seafood </option>
</SELECT>
<Select class = "dropdownlist" id = "categoryname" name = "categoryname">
<Option value = ""> -- select one -- </option>
<Option value = "1"> beverages </option>
<Option value = "2"> condiments </option>
<Option value = "3"> confections </option>
<Option value = "4"> dairy products </option>
<Option value = "5"> grains/cereals </option>
<Option value = "6"> meat/poultry </option>
<Option value = "7"> produce </option>
<Option value = "8"> seafood </option>
</SELECT>
15. Partial View Template
Webform is called a custom control. All functions are for reuse. However, it is really difficult to use custom controls.
<% Html. renderpartial ("dinnerform"); %> check that there is no equal sign.