2009-06-17 33 views
1

試圖創建此擴展方法。除了幫助程序正在呈現文本,而不是在頁面呈現時對視圖的控制之外的作品:ASP.NET MVC助手擴展方法不將文本呈現爲控件

我包含使用System.Web.Mvc.Html;在包含此擴展方法的助手類的頂部,以便它能理解helper.RadioButton。

public static string WriteTestControlToScreen(this HtmlHelper helper) 
{ 
    StringBuilder fields = new StringBuilder(); 
    fields.Append("<fieldset>"); 
    fields.Append("  <div class='formLabel'><span class='requiredText'>*</span><label>Background Color</label></div>"); 
    fields.Append("  <div class='formField'>" + helper.RadioButton("rbBackgroundColorWhite", 0, false) + "<label class='fieldInlineLabel' for=''>White</label></div>"); 
    fields.Append("  <div class='formField'>" + helper.RadioButton("rbBackgroundColorWhite", 0) + "<label class='fieldInlineLabel' for=''>Black</label></div>"); 
    fields.Append("</fieldset>"); 

    return fields.ToString(); 
} 

輸出的查看,然後看起來是這樣的(注意,這不是渲染一個單選按鈕,但把它當作文本代替):

*背景顏色 <%= Html.RadioButton( 'rbBackgroundColorWhite',0 ,FALSE)%>白 <%= Html.RadioButton(「rbBackgroundColorWhite」,0)%>黑色

+0

你是如何調用WriteTestControlToScreen的? – 2009-06-17 03:06:22

+0

<%= Html.WriteTestControlToScreen()%> – PositiveGuy 2009-06-17 03:18:20

回答

1

我做了一點亂抓週圍反射,我注意到,對於輸入MVC擴展方法字段都使用InputHelper類,它反過來使用TagBuilder類:

private static string InputHelper(this HtmlHelper htmlHelper, InputType inputType, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, IDictionary<string, object> htmlAttributes) 
{ 
    ModelState state; 
    if (string.IsNullOrEmpty(name)) 
    { 
     throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name"); 
    } 
    TagBuilder builder = new TagBuilder("input"); 
    builder.MergeAttributes<string, object>(htmlAttributes); 
    builder.MergeAttribute("type", HtmlHelper.GetInputTypeString(inputType)); 
    builder.MergeAttribute("name", name, true); 
    string str = Convert.ToString(value, CultureInfo.CurrentCulture); 
    bool flag = false; 
    switch (inputType) 
    { 
     case InputType.CheckBox: 

       //...etc. 

以下是幾種涵蓋各種輸入類型所需的額外渲染的案例陳述。但你明白了。 MVC中的人不串聯字符串;他們正在使用MergeAttribute(和其他)方法來爲他們做骯髒的工作。我的猜測是在那裏有一些瀏覽器兼容性的好處。

我的建議是,你可以使用TagBuilder類來構建你的HTML,就像MVC人一樣。

+0

但最後,他們不是隻是返回一個字符串?即返回tagBuilder.ToString(TagRenderMode.SelfClosing); – 2009-06-17 03:08:38

1

原始代碼的問題在於,當MVC需要文字html輸出時,您正在輸出<%=%>標籤。換句話說,它不處理這些標籤。

請考慮將您的HTML放入.ASCX文件並執行RenderPartial

1

我知道這是一個較舊的帖子,但有一個優雅的解決方案,當我想要在另一箇中呈現Telerik MVC控件時使用過。但是,它可以用於任何HTML助手。它涉及到在CSHTML(剃刀)文件創建一個內聯幫手,如下圖所示:

<!-- This helper avoids nested literals with the sub-panel item --> 
@helper RenderPositionalResponsibilities() 
{ 
    Html.Telerik().PanelBar() 
     .Name("ContactSubItem") 
     .Items(subitem => 
     { 
      subitem.Add() 
       .Text("Positional Responsibilities") 
       .Content(
        @<text> 
         <div class="container scrollable" style="height: 150px;"> 
          @Html.CheckboxFor(model => model.IsTrue) 
         </div> 
        </text> 
       ) 
       .Expanded(true); 
     }) 
     .Render(); 
} 

如下圖所示的含量的方法,然後它可以被稱爲:

@{Html.Telerik().PanelBar() 
.Name("pnlUserForm") 
.ClientEvents(e => e.OnExpand("pnlUserForm_OnExpand").OnCollapse("pnlUserForm_OnCollapse")) 
.Items(i => 
{ 
    i.Add() 
     .Text("User Information") 
     .Expanded(true) 
     .Content(
      @<text> 
       <div> 
        @RenderPositionalResponsibilities() 
       </div> 
      </text> 
     ) 
} 
.Render(); 
} 

這可能是一個更好匹配希望爲特定目的創建助手的用戶。另外,由於您在cshtml文件(對於剃鬚刀)中創建了幫助程序,因此您可以獲得控件的所有智能感知。