我试图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
我很感激你建议。
错误信息应该是显而易见的。第三种方法使用7个参数调用ExCheckBox。你需要另一个接受7个参数的帮助器 – 2014-11-21 07:19:21
你甚至有一种方法可以实际呈现任何html吗? – 2014-11-22 01:09:29
我使用以下: @ Html.ExCheckBox2(“旋转”,Model.Client.Rotates,(布尔)this.ViewData [“只读”],“”) – hncl 2014-11-22 03:48:43