如果FlagAccessEdit = false,如何禁用或只读?
public static MvcHtmlString CCheckBox(this HtmlHelper htmlHelper, string name,object htmlAttributes, bool FlagAccessEdit = true, bool FlagAccessView = true) { if (!FlagAccessView) return MvcHtmlString.Empty; else if (!FlagAccessEdit && FlagAccessView) { return htmlHelper.CheckBox(name, htmlAttributes); } else return htmlHelper.CheckBox(name, htmlAttributes); }
答案 0 :(得分:0)
您需要获取现有的htmlAttribute,并根据您的情况添加禁用或只读。以下是正确的代码
帮助方法
public static MvcHtmlString CCheckBox(this HtmlHelper htmlHelper,
string name, object htmlAttributes,bool FlagAccessEdit = true,
bool FlagAccessView = true)
{
//get the htmlAttribute
IDictionary<string, object> attributes = new RouteValueDictionary(htmlAttributes);
if (!FlagAccessView)
return MvcHtmlString.Empty;
else if (!FlagAccessEdit && FlagAccessView)
{
//Add the disabled attribute
attributes.Add("disabled", "disabled");
return htmlHelper.CheckBox(name, attributes);
}
else
{
return htmlHelper.CheckBox(name, htmlAttributes);
}
}
调用以下方法
@Html.CCheckBox("chkCheckbox", new { id="chkDemo"},false,true)