ASP.NET MVC - مساعدات HTML

تستخدم مساعدات HTML تعديل输出版本 HTML.

مساعدات HTML

من خلال MVC،تبدو مساعدات HTML مثل عناصر التحكم التقليدية في ASP.NET Web Form.

مثل عنصر التحكم في نموذج الويب ASP.NET،تستخدم مساعدات HTML تعديل HTML. ولكن مساعدات HTML أخف. على عكس عناصر التحكم في نموذج الويب،لا تحتوي مساعدات HTML على نموذج الأحداث و view state.

في معظم الحالات،المساعدات HTML هي مجرد طرق تعود النصوص.

من خلال MVC،يمكنك إنشاء مساعدات خاصة بك أو استخدام المساعدات المدمجة HTML.

مساعدات HTML القياسية

MVC يحتوي على مساعدات قياسية لمعظم أنواع العناصر HTML الشائعة، مثل رابط HTML و عناصر أشكال HTML.

رابط HTML

أسهل طريقة لعرض رابط 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>

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