MVC 2 EditorForModel 指定生成输入的宽度

发布于 2024-10-02 01:26:38 字数 275 浏览 0 评论 0原文

我在视图中使用 Html.EditorForModel() 来生成编辑表单的字段。我还有另一个部分类,我在其中为某些字段指定一些数据注释属性(如 DisplayName、Range 等)。

当我运行该应用程序时,我为每个字段生成了 HTML 输入。如何指定生成的输入的宽度?

像这样的东西:

<input id="nameTextBox" style="width:220px" name="theName" />

I use the Html.EditorForModel() in a view to generate fields for the edit form. I also have another partial class where I specify some Data Annotation attributes for some fields (like DisplayName, Range etc).

When I run the application I have HTML inputs generated for each field. How can I specify the width of this generated inputs ?

Something like this:

<input id="nameTextBox" style="width:220px" name="theName" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2024-10-09 01:26:38

@Gerrie 的解决方案有效,但它适用于页面上的所有输入。如果您不想以这种方式设置页面上的其他输入,那么您可以执行以下操作:

<div id="model-editor">
    <%: Html.EditorForModel() %>
</div>

然后使用以下形式的 CSS:

#model-editor input { width:220px; }

编辑

如果您想设置 CSS 规则对于个别领域,您也可以这样做。检查生成的 HTML 并找到输入的 id,然后执行以下操作:

input[id='YourInputId'] { width:400px; }

@Gerrie's solution works, but it applies to all inputs on the page. If you have other inputs on the page that you don't want to style this way, then you can do something like this:

<div id="model-editor">
    <%: Html.EditorForModel() %>
</div>

And then use CSS of this form:

#model-editor input { width:220px; }

EDIT

If you want to set CSS rules for individual fields, you can do so too. Inspect the generated HTML and find the input's id, then do:

input[id='YourInputId'] { width:400px; }
薄暮涼年 2024-10-09 01:26:38

使用 CSS:

input
{
    width: 220px;
}

这样您就不必费心将这些样式属性插入到生成的控件中。

use CSS:

input
{
    width: 220px;
}

This way you don't have to bother about inserting these style attributes into the generated controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文