我可以更改带有数据注释的 EditorFor 的宽度吗?
Can I change the width of an EditorFor with data annotations?
有几个关于类似主题的问题。但是,他们实际上并没有解决我的问题。
DisplayFormat for TextBoxFor in MVC
Display a formatted date in an 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;" })
我明白了:
我要的是滚动条,但是要格式化宽度
我想知道,有没有办法用 data annotations
格式化 EditorFor
的宽度?如果不是,制作这种格式的最简单方法是什么,以避免重复.. bootstrap
, css
?我对内联样式不满意。
根据评论,您使用的是 MVC-4。您不能将 html 属性传递给 EditorFor()
方法,除非您使用 MVC-5.1 或更高版本(请参阅 release notes - 关于 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()
方法呈现所有元素。
有几个关于类似主题的问题。但是,他们实际上并没有解决我的问题。
DisplayFormat for TextBoxFor in MVC
Display a formatted date in an 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;" })
我明白了:
我要的是滚动条,但是要格式化宽度
我想知道,有没有办法用 data annotations
格式化 EditorFor
的宽度?如果不是,制作这种格式的最简单方法是什么,以避免重复.. bootstrap
, css
?我对内联样式不满意。
根据评论,您使用的是 MVC-4。您不能将 html 属性传递给 EditorFor()
方法,除非您使用 MVC-5.1 或更高版本(请参阅 release notes - 关于 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()
方法呈现所有元素。