1

有几个关于类似主题的问题。但是,它们实际上并没有解决我的问题。

MVC 中 TextBoxFor 的显示格式

在 EditorFor() 中显示格式化的日期

ASP.NET MVC EditorFor DateTime

我一直在搜索和阅读,但找不到答案。

我目前正在使用它来格式化我的输入框:

@Html.TextBoxFor(modelItem => item.Quantity, new { style = "width: 30px;" })

从我的模型中,我得到了简短的数据注释:

[Required, Range(1, 100)]
public int Quantity { get; set; }

所以我明白了:

在此处输入图像描述

当我使用

@Html.EditorFor(modelItem => item.Quantity, new { style = "width: 30px;" })

我明白了:

在此处输入图像描述

我想要的是滚动,但要格式化宽度。

我想知道,有没有办法格式化EditorForwith的宽度data annotations?如果不是,那么制作这种格式的最简单方法是什么,以避免重复.. bootstrapcss?我对内联样式不满意。

4

1 回答 1

2

从评论中,您使用 MVC-4。EditorFor()除非您使用 MVC-5.1 或更高版本,否则您不能将 html 属性传递给该方法(请参阅发行说明-编辑器模板的 Bootstrap 支持部分)

一种选择是使用TextBoxFor()方法和传递type="number"来呈现浏览器数字控件

@Html.TextBoxFor(m => m.Quantity, new { type = "number ", style = "width: 30px;" })

另一个是创建一个自定义EditorTemplate(比如_NumericControl.cshtml

@model System.Int32
@Html.TextBoxFor(m => m, new { type = "number ", style = "width: 30px;" })

并使用@Html.EditorFor(m => m.Quantity, "_NumericControl")

但这实际上只有在模板还包含其他 html 元素时才有真正的好处,例如@Html.LabelFor(m => m)@Html.ValidationMessageFor(m => m)甚至可能包含关联的按钮,以便该EditorFor()方法呈现所有元素。

于 2015-08-13T02:46:08.213 回答