在mvc页面中验证隐藏字段的最佳方法是什么?

时间:2023-01-26 19:40:38

How can I enforce Form to validate a hidden field in a MVC page?

如何执行表单来验证MVC页面中的隐藏字段?

it is my View:

这是我的观点:

@Html.HiddenFor(model => model.t)
@Html.ValidationMessageFor(model => model.t)

1 个解决方案

#1


2  

Hidden fields are excluded from validation. On the other hand you could use a normal field that you would simply visually hide in your CSS:

隐藏字段被排除在验证之外。另一方面,您可以使用一个普通的字段,您可以简单地将其隐藏在您的CSS中:

@Html.TextBoxFor(model => model.t, new { @class = "hidden" })
@Html.ValidationMessageFor(model => model.t)

and in your CSS:

和你的CSS:

.hidden {
    display: none;
}

#1


2  

Hidden fields are excluded from validation. On the other hand you could use a normal field that you would simply visually hide in your CSS:

隐藏字段被排除在验证之外。另一方面,您可以使用一个普通的字段,您可以简单地将其隐藏在您的CSS中:

@Html.TextBoxFor(model => model.t, new { @class = "hidden" })
@Html.ValidationMessageFor(model => model.t)

and in your CSS:

和你的CSS:

.hidden {
    display: none;
}