简体   繁体   English

mvc 3 Html.EditorFor 添加 html 属性不起作用

[英]mvc 3 Html.EditorFor add html attribute not work

I try add html attribute for EditorFor我尝试为 EditorFor 添加 html 属性

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

But any way I get但是我得到的任何方式

<input id="UserName" class="text-box single-line" type="text" value="" name="UserName">

EditorFor is going to overwrite those attributes. EditorFor 将覆盖这些属性。 See Html attributes for EditorFor() in ASP.NET MVC for workarounds.有关解决方法,请参阅ASP.NET MVC 中 EditorFor() 的 Html 属性

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如果您只需要更改宽度的显示,并且您不依赖任何编辑器模板,那么最简单的解决方法是改用 TextBoxFor

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

I had the same issue and this solved it...我有同样的问题,这解决了它......

<style type="text/css">
#UserName { 
    width: 100px; 
}
</style>

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

@Html.EditorFor() dynamically decides the type of control used based on the model element. @Html.EditorFor()根据 model 元素动态决定使用的控件类型。

These customizations work with @Html.LabelFor() of @Html.TextBoxFor() .这些自定义与@Html.LabelFor()@Html.TextBoxFor()一起使用。 The following code has been testes and works properly.以下代码已经过测试并且可以正常工作。

Razor: Razor:

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

Generated HTML生成的 HTML

<label style="display: inline;" for="UserName">
<input name="LastActivityDate" id="UserName" style="display: inline;" type="text" value=""/>

Please note that the second argument passed is null , style is the third attribute.请注意,传递的第二个参数是null ,样式是第三个属性。 if using @Html.EditorFor() is a must, then you have to use CSS class instead of style如果必须使用@Html.EditorFor() ,那么您必须使用 CSS class 而不是 style

@Html.EditorFor() on MSDN MSDN上的@Html.EditorFor()

Note: The code has been tested with MVC4 and hopefully it is valid for MVC3 also注意:代码已经用 MVC4 测试过,希望它对 MVC3 也有效

Instead of using而不是使用
@Html.EditorFor(model => model.UserId) @Html.EditorFor(model => model.UserId)
Use利用
@Html.TextBoxFor(model => model.UserId, new { @Value = "Prabhat" }) @Html.TextBoxFor(模型 => model.UserId, 新 { @Value = "Prabhat" })

For what it's worth I had this same problem.对于它的价值,我遇到了同样的问题。 I resolved it by using a slightly different version of Narenda V's solution.我通过使用稍微不同版本的 Narenda V 解决方案解决了这个问题。

I created a class like this: (note - min-width instead of width)我像这样创建了一个 class :(注意 - 最小宽度而不是宽度)

.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.当我不使用 min-width,但使用宽度时,它不起作用。 Bob鲍勃

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:如果您需要使用 EditorFor 而不是 TextBoxFor 并且页面上有多个编辑器,但只需要更改特定的宽度...最简单的方法是将 css 样式添加到 class ID 如下:

<style type="text/css">
    input#UserName { width: 100px; }
</style>

You can create a style class in.css like您可以在 css 中创建一个样式 class 像

.userName
{
   width: 150px;
}

Style can be appilied with html class attribute.样式可以应用 html class 属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM