我有以下定义:
public class Menu
{
[DisplayName("Control")]
public int Control { get; set; }
像这样使用它:
@Html.TextBoxFor(x => x.Control, new { id = "control" })
我可以指定订单的范围吗?我希望订单成为一个 数字在0到999之间。
我也可以让创建的文本框只有三个空格 字符?
答案 0 :(得分:5)
Is it possible for me to specify a range for the order? I would like the order to be a number between 0 and 999.
[DisplayName("Control")]
[Range(0, 99, ErrorMessage = "{0} must be between {1} and {2}")]
public int Control { get; set; }
关于第二个问题
Also is it possible for me to make the textbox that gets created just have space for three characters?
[StringLength(3, ErrorMessage="{0} must be {1} characters"]
[DisplayName("Control")]
[Range(0, 99, ErrorMessage = "{0} must be between {1} and {2}")]
public int Control { get; set; }
如果需要,您还可以指定minumun chars长度
[StringLength(10, minimumLength=5, ErrorMessage="{0} must be between {1} and {2} characters"]
public int Control { get; set; }
答案 1 :(得分:3)
您可以使用此范围
public class Menu
{
[DisplayName("Control")]
[Range(0, 999)]
public int Control { get; set; }
}
只有三个空间使用此
@Html.TextBoxFor(x => x.Control, new { id = "control",maxlength="3" })
答案 2 :(得分:2)
在这种情况下,尝试使用范围注释。
public class Menu
{
[DisplayName("Control")]
[Range(0, 999, ErrorMessage = "Control should be between 0 and 999")]
public int Control { get; set; }
}
是的,以及文本框的长度
@Html.TextBoxFor(x => x.Control, new { id = "control", maxlength="3" })