mvc3 @html.editorfor,mvc 3 Html.EditorFor add html attribute not work

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I try add html attribute for EditorFor

@Html.EditorFor(model => model.UserName, new { style = "width: 100px" })

But any way I get

回答1:

EditorFor is going to overwrite those attributes. See Html attributes for EditorFor() in ASP.NET MVC for workarounds.

If all you need to change is the display of the width, and you're not relying on any Editor Templates then the easiest fix is to use TextBoxFor instead

@Html.TextBoxFor(model => model.UserName, new { style = "width: 100px" })

回答2:

I had the same issue and this solved it...

#UserName {

width: 100px;

}

@Html.EditorFor(model => model.UserName)

回答3:

@Html.EditorFor() dynamically decides the type of control used based on the model element.

These customizations work with @Html.LabelFor() of @Html.TextBoxFor(). The following code has been testes and works properly.

Razor:

@Html.LabelFor(model => model.UserName, null, new { style = "display: inline;" })

@Html.TextBoxFor(model => model.UserName, null, new { style = "display: inline;" })

Generated HTML

Please note that the second argument passed is null, style is the third attribute. if using @Html.EditorFor()is a must, then you have to use CSS class instead of style

@Html.EditorFor() on MSDN

Note: The code has been tested with MVC4 and hopefully it is valid for MVC3 also

回答4:

Instead of using

@Html.EditorFor(model => model.UserId)

Use

@Html.TextBoxFor(model => model.UserId, new { @Value = "Prabhat" })

回答5:

For what it's worth I had this same problem. I resolved it by using a slightly different version of Narenda V's solution.

I created a class like this: (note - min-width instead of width)

.userName { min-width:40em; }

then in my view:

@Html.EditorFor(model => model.UserName, new { @class = "userName" })

When I did not use min-width, but width, it did not work.

Bob

回答6:

If you need to use EditorFor and not TextBoxFor and have multiple editors on the page, but only need to change the width of specific ones... The easiest what is to add a css style to the class ID as such:

input#UserName { width: 100px; }

回答7:

You can create a style class in .css like

.userName

{

width: 150px;

}

Style can be appilied with html class attribute.

@Html.EditorFor(model => model.UserName, new { @class = "userName" })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值