//NOTE: SOME CONTENT OMITTED FROM THIS SNipPET } NewText方法有四個不同版本的重載,這些重載都屬于System.Web.Mvc.HtmlHelper的擴展方法,只有最后一個方法用于真正的起作用,而其他的方法都是這個方法的封裝以便讓用戶使用起來更簡單.上面的代碼中HtmlText對象通過傳入適當的參數來初始化,而view是通過StartView方法來初始化,在StartView中被調用的HtmlText會返回合適的對象動態的將Html注入View.現在讓我們來看看如何在view中使用這些方法。
// Write out the radio buttons, let the MVC Helper do the hard work here foreach (SelectListItem item in this.mSelectList) { string text = !string.IsNullOrEmpty(item.Text) ? item.Text : item.Value;
// Note: Because we are using HtmlHelper.RadioButton the <input> // elements will have duplicate ids // See: http://forums.asp.net/t/1363177.aspx // In order to avoid this we could do this ourselves here TagBuilder spanTag = new TagBuilder("span"); spanTag.SetInnerText(text); httpResponse.Write(spanTag.ToString(TagRenderMode.Normal)); }