004.ASP.NET MVC中的HTML Helpers

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   art   

原文連結:http://www.codeproject.com/Articles/794579/ASP-NET-MVC-HTML-Helpers-A-MUST-KNOW

1.什麼是HTML Helpers

  想像HTML Helpers的方法返回一個字串,是的,它返回的字串是帶有HTML標籤的字串。比如一個img標籤、一個a標籤,等其它的標籤。

  有ASP.NET開發經驗的人開發人員能使用HTML Helpers用於Web Form伺服器控制項的編寫,因為它們兩有一個共同的目標。但是HTML Helpers更加輕量級,因為它沒有檢視狀態和事件模型,單獨使用HTML Helpers,我們可以建立自訂的helpers的方法。

2.HTML Helpers的標準

  對於ASP.NET MVC開發人員,理解HTML Helpers是十分可取的。

  標準的HTML Helpers能分類成下面兩種:

    a. URL Heplers = HTML Links + Image Links

    b. HTML Form Elements

3.MVC URL Helpers

  在MVC中提供連結是必需的,有兩種類型可用:HTML Links和Image Links,我們分別來討論它們.

  a. HTML Links

  Html.ActionLink() 協助方法用於在一個視圖中返回HTML Link,該方法可以連結到一個控制器的方法到視圖中.

1 @Html.ActionLink(“顯示的文本”,”actionName”)

  上面的方法是Html.ActionLink的一個重載,可以返回一個<a>標籤,連結到控制器的actionName方法

1 <a href=”/ControllerName/actionName”>顯示的文本</a>

  b. Image Links

  Url.Action()協助方法可了返回一個Image連結

1 <a href=”@Url.Action(“ViewDetails”)”><img src=”./logo.jpg” alt=”View Details” /></a>

  注意:儘管Image Link做了同樣的事情,連結到一個控制器的ViewDetails方法,但是如果使用Html.ActionLink方法就不能包含一個img標籤

4.MVC HTML Form Element

  為了返回HTML中各種元素的標籤,MVC為我提供下以下一些HTML協助方法

@Html.TextBox("strStudentName") renders:<input id="strStudentName" name="strStudentName" type="text" value="" />@Html.TextArea("strAcademicBackground", "", 10, 50, null) renders:<textarea cols="50" id="strAcademicBackground" name="strAcademicBackground" rows="10">@Html.Password("strPassword") renders:<input id="strPassword" name="strPassword" type="password" />@Html.RadioButton("radGender", "Male", true) renders:<input checked="checked" id="radGender" name="radGender" type="radio" value="Male" />@Html.CheckBox("chkDuesPaid", true) renders:<input checked="checked" id="chkDuesPaid" name="chkDuesPaid" type="checkbox" value="true" /><input name="chkDuesPaid" type="hidden" value="false" />@Html.DropDownList ("ddlLevel", new SelectList(new [] {"1st Grade", "2nd Grade", "3rd Grade"})) renders:<select id="ddlLevel" name="ddlLevel">     <option>1st Grade</option>     <option>2nd Grade</option>     <option>3rd Grade</option></select>

  這篇文章只能算是一些用法的列舉,應當做忘記時來看看的小例子吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.