ASP.NET MVC - HTML-помощники
- Предыдущая страница Безопасность MVC
- Следующая страница Развертывание MVC
HTML-помощники используются для изменения вывода HTML.
HTML-помощники
Через MVC, HTML-помощники аналогичны традиционным ASP.NET Web Form控件ам.
Аналогично web form控件 в ASP.NET, HTML-помощники используются для изменения HTML. Но HTML-помощники легче. В отличии от web form控件, HTML-помощники не имеют модели событий и view state.
В большинстве случаев HTML-помощники представляют собой методы, возвращающие строку.
Через MVC, вы можете создавать свои собственные помощники или использовать встроенные HTML-помощники.
Стандартные HTML-помощники
MVC включает в себя большинство стандартных помощников для типов HTML-элементов, таких как HTML-ссылки и элементы HTML-формы.
HTML-ссылка
Самый простой способpresentation HTML-ссылки - это использование помощника Html.ActionLink().
Через MVC, Html.ActionLink() не соединяется с видом. Он создает связь с 操作ом контроллера (controller action).
Razor грамматика:
@Html.ActionLink("About this Website", "About")
ASP грамматика:
<%=Html.ActionLink("About this Website", "About")%>
The first parameter is the link text, and the second parameter is the name of the controller action.
The above Html.ActionLink() helper outputs the following HTML:
<a href="/Home/About">About this Website</a>
Several parameters of the Html.ActionLink() helper:
Parameter | Description |
---|---|
linkText | The internal text of the anchor element. |
actionName | The name of the action. |
controllerName | The name of the controller. |
protocol | The protocol of the URL, such as "http" or "https". |
hostname | The hostname of the URL. |
fragment | URL fragment name (anchor name). |
routeValues | An object that contains route parameters. |
htmlAttributes | An object that contains the HTML attributes to be set for the element. |
Note:You can pass values to controller actions. For example, you can pass the id of a database record to a database editing operation.
Razor syntax C#:
@Html.ActionLink("Edit Record", "Edit", new {Id=3})
Razor syntax VB:
@Html.ActionLink("Edit Record", "Edit", New With{.Id=3})
The above Html.ActionLink() helper outputs the following HTML:
<a href="/Home/Edit/3">Edit Record</a>
HTML form elements
The following HTML helpers can be used to display (modify and output) HTML form elements:
- BeginForm()
- EndForm()
- TextArea()
- TextBox()
- CheckBox()
- RadioButton()
- ListBox()
- DropDownList()
- Hidden()
- Password()
ASP.NET syntax C#:
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %> <% using (Html.BeginForm()){%> <p> <label for="FirstName">First Name:</label> <%= Html.TextBox("FirstName") %> <%= Html.ValidationMessage("FirstName", "*") %> </p> <p> <label for="LastName">Фамилия:</label> <%= Html.TextBox("LastName") %> <%= Html.ValidationMessage("LastName", "*") %> </p> <p> <label for="Password">Пароль:</label> <%= Html.Password("Password") %> <%= Html.ValidationMessage("Password", "*") %> </p> <p> <label for="Password">Подтвердите пароль:</label> <%= Html.Password("ConfirmPassword") %> <%= Html.ValidationMessage("ConfirmPassword", "*") %> </p> <p> <label for="Profile">Профиль:</label> <%= Html.TextArea("Profile", new {cols=60, rows=10})%> </p> <p> <%= Html.CheckBox("ReceiveNewsletter") %> <label for="ReceiveNewsletter" style="display:inline">Получать новости?</label> </p> <p> <input type="submit" value="Register" /> </p> <%}%>
- Предыдущая страница Безопасность MVC
- Следующая страница Развертывание MVC