ASP.NET MVC - کمک‌کنندگان HTML

کمک‌کنندگان 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

ساده‌ترین روش برای نمایش لینک‌های HTML استفاده از کمک‌کننده Html.ActionLink() است.

از طریق MVC،Html.ActionLink() به دیدگاه متصل نمی‌شود. آن ایجاد یک اتصال به عمل کنترل‌کننده (controller action) می‌کند.

رازور زبان:

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

آسپرسکریپت زبان:

<%=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 Grammar C#:

@Html.ActionLink("Edit Record", "Edit", new {Id=3})

Razor Grammar 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 present (modify and output) HTML form elements:

  • BeginForm()
  • EndForm()
  • TextArea()
  • TextBox()
  • CheckBox()
  • RadioButton()
  • ListBox()
  • DropDownList()
  • Hidden()
  • Password()

ASP.NET Grammar 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">Newsletter حاصل کرنا؟</label>
</p>
<p>
<input type="submit" value="Register" />
</p>
<%}%>