05 razor & helpers

Page 1

ThS. Nguyễn Nghiệm 0913.745.789 nghiemn@fpt.edu.vn songlong2k@gmail.com


 Razor  MVC Helper

HyperLink Helpers Form Helpers Model Helpers Hiển thị thông tin từ model  Customer Helper



ISBN@isbnNumber


 Helper là các thành phần sinh giao diện web phù

hợp.  Đơn giản việc viết mã sinh giao diện Liên kết Form Sinh giao diện từ model Kiểm lỗi


@Html.ActionLink() được sử dụng để sinh siêu liên kết

Helper @Html.ActionLink() nhận một số tham số:  linkText – nhãn của liên kết  actionName – tên action  routeValues – tập các giá trị truyền đến action.  controllerName – tên controller  htmlAttributes – tập thuộc tính HTML của thể <a>

Ví dụ:

Sinh liên kết ảnh



         

@Html.TextBox("txtFrom") @Html.Password("txtPassword") @Html.TextArea("txtNotes") @Html.RadioButton("rdoGender", "Male") @Html.CheckBox("chkReading") @Html.Hidden("hidCountry") @Html.DropDownList("ArtistId", String.Empty) @Html.ListBox("ArtistId") @Html.Label("chkMale", "Is Male ?") Chú ý: Với Html.DropDownList() và Html.ListBox() cần nguồn dữ liệu từ ViewBag hoặc ViewData. Sau đây là 1 ví dụ: ViewBag.ArtistId = new SelectList(ArtistList, "ArtistId", "Name");







 

@Html.FormatValue (value, format) @String.Format(format, value1, value2…)  {0:C}: Currency  {0:P}: Percent  {0:D}: Date  {0:MMMM-dd-yyyy hh:mm:ss tt}: DateTime  M,MM,MMM,MMM  d,dd  yy,yyyy  H, HH, h, hh  m,mm  s,ss  tt

 {0:#,###.##0}: Number  

@Html.Encode (html) @Html.Raw (html)


 @Html.Encode(HtmlString)

<  < >  > “  " &  & <script>alert(‘Boom!’)</script>  <script>alert(‘Boom!’)</script>  @Html.Decode(HtmlString)

Giải mã ngược với @Html.Encode()


ThS. Nguyễn Nghiệm 0913.745.789 nghiemn@fpt.edu.vn songlong2k@gmail.com


 @Html.TextBoxFor (m => m.Property)

Sinh phần tử textbox cho thuộc tính Property  @Html.PasswordFor (m => m.Property)

Sinh phần tử password cho thuộc tính Property  @Html.TextAreaFor (m => m.Property)

Sinh phần tử texterea cho thuộc tính Property  @Html.CheckBoxFor (m => m.Property)

Sinh phần tử checkbox cho thuộc tính Property  @Html.RadioButtonFor

(m => m.Property)

Sinh phần tử radio cho thuộc tính Property


 @Html.HiddenFor

(m=> m.Property)

Sinh phần tử hidden cho thuộc tính Property  @Html.DropDownListFor

(m=> m.Property)

Sinh phần tử select-one cho thuộc tính Property  @Html.ListBoxFor (m=> m.Property)

Sinh phần tử select-multiple cho thuộc tính Property  @Html.LabelFor (m=> m.Property)

Sinh phần tử label cho thuộc tính Property


 @Html.EditorFor (m => m.Property)

Sinh phần tử nhập cho thuộc tính Property của model  @Html.EditorForModel ()

Sinh tất cả các phần từ nhập cho tất cả các thuộc tính của model  @Html.Editor

("Mail")

Sinh tất cả các phần từ nhập cho tất cả các thuộc tính của đối tượng Mail trong ViewData hay ViewBag


@Html.DisplayNameFor (m=> m.Property) Hiển thị tên của thuộc tính Property

@Html.DisplayTextFor (m=> m.Property) Hiển thị giá trị của thuộc tính Property

@Html.LabelFor (m=> m.Property) Sinh label cho thuộc tính Property

@Html.DisplayFor (m=>m.Property) Hiển thị giá trị cho thuộc tính Property

@Html.DisplayForModel () Hiển thị giá trị của tất cả các thuộc tính

@Html.Display ("Mail") Hiển thị giá trị của tất cả các thuộc tính của đối tượng trong ViewData hay ViewBag



 Lý do: Số lượng HtmlHelper vô cùng ít ỏi đòi hỏi

bạn phải định nghĩa thêm.  Có 3 cách định nghĩa thêm helper là Định nghĩa phương thức tĩnh Sử dụng: @String.Format("{0:F}", Model.Price)

Định nghĩa phương thức mở rộng của HtmlHelper Sử dụng: @Html.Submit(String label)

Sử dụng chỉ thị @helper để định nghĩa trực tiếp trên View Sử dụng: @Trancate(title, 20)


???



Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.