c#标签添html加属性,c# - 如何通过html helper方法更改属性或向html字段添加属性? - 堆栈内存溢出...

我想基于布尔值禁用或启用文本框,我创建了此扩展方法:

public static IHtmlString MyTextBoxFor(

this HtmlHelper htmlHelper,

Expression> expression,

object htmlAttributes,

bool disabled

)

{

var attributes = new RouteValueDictionary(htmlAttributes);

if (disabled)

{

attributes.Add("disabled", "\"disabled\"");

}

return htmlHelper.TextBoxFor(expression, htmlAttributes);

}

那就是我的用法:

@Html.MyTextBoxFor(model => model.Body, new { @class = "form-control"}, true)

但是它不起作用,我不熟悉Htmlhelper类,虽然不难理解,但是我确实错过了一些东西!

编辑:

我尝试了这种简单的方法,以找出问题所在:

public static IHtmlString MyTextBox(this HtmlHelper htmlHelper,object htmlAttributes, bool disabled)

{

IDictionary attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

//var attrs = new Dictionary();

if (disabled)

{

attrs.Add("disabled", "disabled");

attrs.Add("value", "txxxxxxt");

}

return htmlHelper.TextBox("txtbx", attrs);

}

并且已经呈现出来:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值