如何为动态对象添加其他属性?

时间:2016-04-10 13:26:11

标签: asp.net-mvc c#-4.0

如果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);
}

1 个答案:

答案 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)