ASP.NET MVC - HTML-Hilfsmodule

HTML-Hilfsmodule werden verwendet, um HTML-Ausgaben zu ändern.

HTML-Hilfsmodule

Durch MVC sind HTML-Hilfsmodule ähnlich wie traditionelle ASP.NET Web Form Controls.

Ähnlich wie Web Form Controls in ASP.NET werden HTML-Hilfsmodule verwendet, um HTML zu ändern. Im Gegensatz zu Web Form Controls haben HTML-Hilfsmodule jedoch kein Event-Modell und kein View State.

In den meisten Fällen sind HTML-Hilfsmodule einfach Methoden, die Zeichenfolgen zurückgeben.

Durch MVC können Sie eigene Hilfsmodule erstellen oder die integrierten HTML-Hilfsmodule verwenden.

Standard-HTML-Hilfsmodule

MVC enthält Standardhilfsmodule für die meisten gängigen HTML-Elementtypen, wie z.B. HTML-Links und HTML-Formularelemente.

HTML-Links

Die einfachste Methode zur Darstellung eines HTML-Links ist die Verwendung des Html.ActionLink() Hilfsmoduls.

Durch MVC wird Html.ActionLink() nicht auf die Ansicht verlinkt. Es erstellt eine Verbindung zum Controller-Action.

Razor-Syntax:

@Html.ActionLink("About this Website", "About")

ASP-Syntax:

<%=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

Following HTML helpers can be used to render (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">Nachname:</label>
<%= Html.TextBox("LastName") %>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<p>
<label for="Password">Passwort:</label>
<%= Html.Password("Password") %>
<%= Html.ValidationMessage("Password", "*") %>
</p>
<p>
<label for="Password">Passwort bestätigen:</label>
<%= Html.Password("ConfirmPassword") %>
<%= Html.ValidationMessage("ConfirmPassword", "*") %>
</p>
<p>
<label for="Profile">Profil:</label>
<%= Html.TextArea("Profile", new {cols=60, rows=10})%>
</p>
<p>
<%= Html.CheckBox("ReceiveNewsletter") %>
<label for="ReceiveNewsletter" style="display:inline">Newsletter erhalten?</label>
</p>
<p>
<input type="submit" value="Register" />
</p>
<%}%>