When writing a page that edits data, we usually write the following code
1: <input type= "text" value= ' <%=viewdata["title"%> ' name= "title"/>
From the previous article we talked about the helper evolution, we think, for this kind of code we can also use a helper from the binding data?
This is of course possible, and ASP. NET MVC provides a htmlhelper for generating HTML tags with data binding.
1.ActionLink
One of the most common is Html.ActionLink
1.1 Basic ways to use
1: <%=html.actionlink ("This is a connection", "Index", "Home")%>
In the case where the urlrouting rule is the default rule, it generates HTML code for
1: <a href= "/" > This is a connection </a>
The three parameters in the ActionLink are
- Text to display
- Action
- Controller
Where the controller can be omitted and pointed to an action under the same controller when omitted.
QueryString and HTML property settings in 1.2ActionLink
1: The wording 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: syntax for 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:querystring and HTML attributes exist at the same time
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 resulting result is:
1: The wording with QueryString
2: <a href= "/?page=1" > This is a connection </a>
3: <a href= "/?page=1" > This is a connection </a>
4: syntax for other HTML attributes
5: <a href= "/? Length=4 "id=" Link1 "> This is a connection </a>
6: <a href= "/" id= "Link1" > This is a connection </a>
7:querystring and HTML attributes exist at the same time
8: <a href= "/?page=1" id= "Link1" > This is a connection </a>
9: <a href= "/?page=1" id= "Link1" > This is a connection </a>
This makes it possible to use ActionLink to generate nearly all of the address connections.
Note that it is not necessary to use ActionLink if the action and Controller are not involved in the connection, but rather to write the HTML code directly, for example
1: <a href= "#1" > Chapter </a>
2: <a href= "javascript:void (0)" onclick= "Delete ();" > Delete </a>
2.routelink2.1 and ActionLink
RouteLink is almost the same as ActionLink, but its address is generated by route generation take the above example
1: <%=html.actionlink ("This is a connection", "Index", "Home")%>
, if you use RouteLink to write
1: <%=html.routelink ("This is a connection", new {controller= "Home", action= "Index"})%>
And the 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, a newly created RouteValueDictionary object (the object instantiated by new{} will be converted to routevaluedictionary equivalent) to specify the original Action,controller string separately.
2.2RouteLink using route rules
In addition to these synergistic uses, RouteLink also supports the use of the route rule name to create a connection
For example, we add a route rule to the Global.asax file
1:routes. MapRoute (
2: "about",//This is the rule name
3: "about",//url
4: New {controller = "Home", action = "About"}
5: );
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: <a href= "/about" > About </a>
2: <a href= "/about?page=1" > About </a>
3: <a href= "/about?page=1" id= "Link1" > About </a>
3. Forms
In many cases it is necessary to generate form elements, as described in the beginning of the article, where we may want to bind the data to the form in the case of a modification of the content.
3.1 Generating a form
We can of course use pure HTML code or Urlhelper to generate a form.
Such as
1: <form action= "/home/index" method= "POST" >
2: </form>
1: <form action= "<%=url.action (" Index "," Home ")%>" method= "POST" >
2: </form>
However, because it is in the HTML attribute, it is still difficult to maintain, fortunately, ASP. NET MVC provides us with a helper, we can build a form in the following two ways:
1: <%using (html.beginform ("index", "Home", FormMethod.Post)) {%>
2: Contents of the form
3: <%}%>
4: <%html.beginform ("index", "Home", formmethod.post);//Note there is no = output%>
5: Contents of the form
6: <%html.endform (); %>
The BeginForm method is similar to how actionlink is called, so ASP. NET MVC also provides the Beginrouteform method.
Of course we can also use new{} to add QueryString or HTML properties to the action of the form, which is similar to the previous description, see the list of methods.
3.2 Form Elements
ASP. NET MVC provides helper for a variety of form elements.
These include: TextBox (similar to input type=text, similar below), TextArea, DropDownList (select), Checkboxhidden, ListBox, Password, Radionbutton.
Note: Because <input type= "submit"/> Generally does not bind data, ASP. NET MVC does not provide this helper (previously provided before PREVIEW2).
If we want to provide an input type=text its name is T1 then the following code:
1: <%=html.textbox ("T1")%>
3.3 Form element bindings
If we want to have a value at the beginning of the T1 above, such as "heavy Pawnage", then we can do it as follows
1: <%=html.textbox ("T1", "heavy Pawnage")%>
If the data is read from the database, that is, the data is obtained from the action, then we can use the ViewData pass in the action
Action:
1:viewdata["name"]= "Heavy Pawnage";
View:
1: <%=html.textbox ("T1", viewdata["name"])%>
The above method seems simple, in fact, ASP. NET MVC provides us with a more convenient way to bind---as long as the ViewData key is guaranteed to be consistent with the name of the helper generated element, it can be automatically bound:
Action:
1:viewdata["T1"]= "heavy Pawnage";
View:
1: <%=html.textbox ("T1")%>
This will automatically bind the
3.4 List data display and binding
It's easier to have a single data like a textbox, but there's more data on how the DropDownList or ListBox should bind data and initialize values, so let's take a look at the following example:
Action:
1:viewdata["Sel1"] = new SelectList (
2: new[] {1, 2, 3}/* List contents can be an array */
3: , 3 */* default value, can be read from the database */
4: );
View:
1: <%=html.dropdownlist ("Sel1")%>
This allows you to bind the list contents, default values, and form elements together.
And our list content is not in any case is an array, most of the case is still key-value to the majority.
We can use the following methods:
1:list<selectlistitem> List = new list<selectlistitem>
2: {
3: new SelectListItem {Text = "heavy Pawnage", Value = "1"},
4: new SelectListItem {Text = "Kingjian", Value = "2"},
5: };
6:viewdata["Sel1"] = new SelectList (
7: List/* Contents can be an array */
8: , "2"/* default value, can be read from the database */
9: );
HtmlHelper Daquan in the MVC2.0