我試圖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