Bài 73: (ASP.NET Razor) Lớp HtmlHelper tạo html trong Razor

Ngày đăng: 12/29/2022 5:04:08 PM

HtmlHelper trong ASP.NET Core

HtmlHelper là lớp có chức năng hỗ trợ dựng HTML (phát sinh mã HTML). Trong ASP.NET Core truy cập được đến đối tượng HtmlHelper tại các View (trang razor .cshtml) với thuộc tính Html

Ví dụ phương thức HtmlHelper.Label trợ giúp phát sinh phần tử <label>. Nếu trong view (.cshtml) viết:

 @Html.Label("inputcontroller", "ĐÂY LÀ LABEL", new {@class = "text-danger"})

Thì kết quả là:

 <label class="text-danger" for="inputcontroller">ĐÂY LÀ LABEL</label>

Có khá nhiều phương thức trong HtmlHelper cho các mục đích sử dụng khác nhau, danh sách đầy đủ tại các phương thức HtmlHelper

Một số phương thức HtmlHelper

Phương thức

Diễn giải

Raw(string)

Giữ nguyên thẻ HTML (không thực hiện encoding), vì mặc định khi xuất một giá trị (@value) thì nó sẽ encoding rồi xuất

Value(expression, format)

Xuất giá trị tên expression (tên liên quan tới model) với chuỗi định dạng format

Encode(value)

Thực hiện Encode chuỗi value.

ActionLink

Tạo thẻ <a> cho các action của controller.

AntiForgeryToken

Tạo phần tử ẩn, nếu trong Form - khi form submit thì nó được kiểm tra, để đảm bảo form được gửi đến từ mã Html do ứng dụng phát sinh

BeginForm

Dựng HTML Form trong MVC

 @using (Html.BeginForm(FormMethod.Post))
 {
     // Các phầ tử
 }
 
BeginRouteForm

Dựng HTML Form trong MVC, action theo Route

CheckBox
CheckBoxFor

 

CheckBox(expression, isChecked, htmlAttributes), Tạo phần tử Html <input> kiểu checkbox, expression chuỗi biểu thức phần tử Model

Display(expression)
DislayFor

 

Dựng HTML cho phần tử Model expression

 

DisplayName(expression)
DisplayNameFor

 

Lấy tên expression, tên thiết lập bằng [Display]

DropDownList
DropDownListFor

DropDownList(expression, selectList, optionLabel, htmlAttributes) tạo phầ tử Select

 @Html.DropDownList("thanhpho",
     new SelectList(new string[] {"Hà Nội", "Sài Gòn"}));
 

 

Editor(expression)
EditorFor

 

Tạo control (phần tử input) cho expression (kiểu quy định type của input)

 

Hidden(expression)
HiddenFor

 

Tạo input có kiểu hidden

 

Label(expression)
LabelFor

 

Tạo phần tử <label>

 

ListBox
ListBoxFor

 

Tạo html select - dùng giống DropDownList

 

PartialAsync

 

Dựng Html từ Partial, xem partial page, partial view

 

RenderPartialAsync

 

Dựng Html từ Partial, xem partial page, partial view

 

Password
PasswordFor

 

Tạo <input> nhập password

 

RadioButton(expression, value)
RadioButtonFor

 

Tạo <input> kiểu radiobutton

 

TextArea
TextAreaFor

 

TextArea(expression, value, rows, columns, htmlAttributes) Tạo <textarea>

 

TextBox
TextBoxFor

 

TextBox(expression, value, format, htmlAttributes) Tạo <input> kiểu text

 

ValidationMessage

 

ValidationMessage (expression, message, htmlAttributes, tag) Trả về HTML thông báo lỗi kiểm tra Model

 

ValidationSummary

Trả về HTML phần tử ul, các thông báo lỗi kiểm tra Model

Có nhiều phương thức dựng phần tử HTML

Nguồn tin: Xuanthulab