2014-11-21 43 views
-2

我试图htmlAttributes添加到现有的帮手:MVC5帮手htmlAttributes

public static MvcHtmlString ExCheckBox (this HtmlHelper helper, string name, bool? value, bool readOnly) 
{ 
    return ExCheckBox(helper, name, value, readOnly, ""); 
} 

public static MvcHtmlString ExCheckBox (this HtmlHelper helper, string name, bool? value, bool readOnly, string Label) 
{ 
    var HTML = ExCheckBox(helper, name, value, readOnly, Label, new Dictionary<string, string>()); 
    return new MvcHtmlString(HTML.ToString()); 
} 

public static MvcHtmlString ExCheckBox4 (this HtmlHelper helper, string name, bool? value, bool readOnly, string Label, object htmlAttributes) 
{ 
    var HTML = ExCheckBox(helper, name, value, readOnly, Label, new RouteValueDictionary(htmlAttributes), new Dictionary<string, string>()); 
    return new MvcHtmlString(HTML.ToString()); 
} 

public static MvcHtmlString ExCheckBox (this HtmlHelper helper, string name, object value, bool readOnly, string Label, IDictionary<string, string> Params) 
    { 
    string HTML = ""; 
    if (readOnly) 
     HTML = String.Format("<label for='{0}'>{1}</label>", name, value == null ? "" : ((bool?)value == true ? "Yes" : "No")); 
    else 
     { 
     string DropDownList = AddEmptyOption(System.Web.Mvc.Html.SelectExtensions.DropDownList(helper, name, GetDropDownListItems(value == null ? "" : value.ToString())).ToString()); 
     HTML += DropDownList; 
     } 
    HTML = AddCellsAndLabel(HTML, name, Label, Params); 
    return new MvcHtmlString(HTML); 
    } 

public static List<SelectListItem> GetDropDownListItems (string currentValue) 
    { 
    var list = new List<SelectListItem>(); 
    var item = new SelectListItem(); 

    item.Text = "No"; 
    item.Value = "false"; 
    if (currentValue == false.ToString()) 
     item.Selected = true; 
    list.Add(item); 
    item = new SelectListItem(); 
    item.Text = "Yes"; 
    item.Value = "true"; 
    if (currentValue == true.ToString()) 
     item.Selected = true; 
    list.Add(item); 

    return list; 
    } 

    private static string AddCellsAndLabel (string ControlHTML, string name, string Label, IDictionary<string, string> Params) 
      { 
      if (!String.IsNullOrEmpty(Label)) 
       { 
       string ControlColSpan = ""; 
       string LabelColSpan = ""; 
       if (Params.ContainsKey(HtmlHelperParams.ControlColSpan)) 
        { 
        ControlColSpan = "colspan='" + Params[HtmlHelperParams.ControlColSpan] + "'"; 
        } 
       if (Params.ContainsKey(HtmlHelperParams.LabelColSpan)) 
        { 
        LabelColSpan = "colspan='" + Params[HtmlHelperParams.LabelColSpan] + "'"; 
        } 
       ControlHTML = String.Format("<td " + LabelColSpan + " class=\"Label\"><label for=\"{0}\">{1}</label></td>", name, Label) + "<td " + ControlColSpan + " class=\"ControlCell\">" + ControlHTML + "</td>"; 
       } 
      return ControlHTML; 
      } 

前两个帮手做工精良,第三ExCheckBox4我得到这个错误:

No overload for method 'ExCheckBox' takes 7 arguments

我很感激你建议。

+0

错误信息应该是显而易见的。第三种方法使用7个参数调用ExCheckBox。你需要另一个接受7个参数的帮助器 – 2014-11-21 07:19:21

+0

你甚至有一种方法可以实际呈现任何html吗? – 2014-11-22 01:09:29

+0

我使用以下: @ Html.ExCheckBox2(“旋转”,Model.Client.Rotates,(布尔)this.ViewData [“只读”],“”) – hncl 2014-11-22 03:48:43

回答

1

代码有很多问题,包括

  1. 它没有强类型
  2. 你重写控件的名称属性,以便它不会绑定到你的 模型上回发
  3. 你在这么假设这是一个for 循环渲染的表用这个,你的渲染复制id(无效HTML)和name 属性(不会在帖子后面绑定到你的收藏)。
  4. 您的呈现<label>元素当它的只读而不是 与控件关联(语义错误)。
  5. 您的分机接受的typeof object的价值,但会抛出 异常,如果它不是nullable bool

请尝试以下

public static MvcHtmlString XCheckBox<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool?>> expression, bool isReadOnly, int labelColumns, int controlColumns, object htmlAttributes) 
{ 
    ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 
    string label = metaData.GetDisplayName(); 
    string name = ExpressionHelper.GetExpressionText(expression); 
    StringBuilder html = new StringBuilder();  
    if (isReadOnly) 
    { 
    html.Append(ReadonlyLabelCell(label, labelColumns)); 
    html.Append(ReadonlyValueCell((bool?)metaData.Model, controlColumns)); 
    } 
    else 
    { 
    html.Append(LabelCell(label, name, labelColumns)); 
    html.Append(ControlCell(name, (bool?)metaData.Model, controlColumns, htmlAttributes)); 
    }  
    return MvcHtmlString.Create(html.ToString()); 
} 

private static string ReadonlyLabelCell(string label, int colSpan) 
{ 
    TagBuilder span = new TagBuilder("span"); 
    span.InnerHtml = label; 
    TagBuilder cell = new TagBuilder("td"); 
    cell.AddCssClass("label"); 
    cell.MergeAttribute("colspan", colSpan.ToString()); 
    cell.InnerHtml = span.ToString(); 
    return cell.ToString(); 
} 

private static string ReadonlyValueCell(bool? value, int colSpan) 
{ 
    TagBuilder span = new TagBuilder("span"); 
    span.InnerHtml = value.HasValue ? value.Value ? "Yes" : "No" : "Not set"; 
    TagBuilder cell = new TagBuilder("td"); 
    cell.AddCssClass("readonly"); // added class for styling 
    cell.MergeAttribute("colspan", colSpan.ToString()); 
    cell.InnerHtml = span.ToString(); 
    return cell.ToString(); 
} 

private static string LabelCell(string labelText, string controlName, int colSpan) 
{ 
    TagBuilder label = new TagBuilder("label"); 
    label.MergeAttribute("for", controlName); 
    label.InnerHtml = labelText; 
    TagBuilder cell = new TagBuilder("td"); 
    cell.AddCssClass("label"); 
    cell.MergeAttribute("colspan", colSpan.ToString()); 
    cell.InnerHtml = label.ToString(); 
    return cell.ToString(); 
} 

private static string ControlCell(string controlName, bool? value, int colSpan, object htmlAttributes) 
{ 
    StringBuilder html = new StringBuilder(); 
    TagBuilder option = new TagBuilder("option"); 
    option.MergeAttribute("value", string.Empty); 
    option.InnerHtml = "Not set"; 
    html.Append(option.ToString()); 
    option = new TagBuilder("option"); 
    option.MergeAttribute("value", "true"); 
    if (value.HasValue && value.Value) 
    { 
    option.MergeAttribute("selected", "selected"); 
    } 
    option.InnerHtml = "Yes"; 
    html.Append(option.ToString()); 
    option = new TagBuilder("option"); 
    option.MergeAttribute("value", "false"); 
    if (value.HasValue && !value.Value) 
    { 
    option.MergeAttribute("selected", "selected"); 
    } 
    option.InnerHtml = "No"; 
    html.Append(option.ToString()); 
    TagBuilder select = new TagBuilder("select"); 
    select.MergeAttribute("name", controlName); 
    select.MergeAttribute("id", HtmlHelper.GenerateIdFromName(controlName)); 
    select.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); 
    select.InnerHtml = html.ToString(); 
    TagBuilder cell = new TagBuilder("td"); 
    cell.AddCssClass("controlcell"); 
    cell.MergeAttribute("colspan", colSpan.ToString()); 
    cell.InnerHtml = select.ToString(); 
    return cell.ToString(); 
} 

然后换另一重载

public static MvcHtmlString XCheckBox<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool?>> expression, bool isReadOnly) 
{ 
    return XCheckBox(helper, expression, isReadOnly, 1, 1, null); 
} 

等。

并使用如下

@Html.XCheckBoxFor(m => m.Client.Rotates, false, 1, 1, new { @class = "form-control" }) 
+0

非常感谢Stephen。 – hncl 2014-11-22 16:40:26

0

很明显 没有法接受这个参数(在ExCheckBox4体)

var HTML = ExCheckBox(helper, name, value, readOnly, Label, new RouteValueDictionary(htmlAttributes), new Dictionary<string, string>()); 
0

您可能需要改变,要

public static MvcHtmlString ExCheckBox4 (this HtmlHelper helper, string name, bool? value, bool readOnly, string Label, object htmlAttributes) 
{ 
var HTML = helper.ExCheckBox(name, value, readOnly, Label, new RouteValueDictionary(htmlAttributes)); 
return new MvcHtmlString(HTML.ToString()); 
}