带下拉菜单的 MVC 和 XVal - 值“请选择” 是无效的
我在使用 xval 验证下拉选择时遇到问题,MVC 绑定似乎推断出错误的事情,允许操作通过验证,但模型状态存储绑定异常并使页面无效 - 所以我得到的情况是操作允许使用无效数据执行,但页面无效,因此用户会认为验证失败。
这可能会更清楚
这是我的下拉菜单,假设用户什么也没做,所以选择了“请选择”。
Do you like cheese:
<select name="Favourites.Cheese">
<option>Please Select</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
<%=Html.ValidationMessage("Favourites.Cheese")%>
这也是来自帖子的控制器操作,MVC 将值 false 映射到 fav.cheese,
public ActionResult Create(Favourites fav)
{
try
{
_favTask.Create(fav);
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, "Favourites");
}
if(!ModelState.IsValid)
{
//redirect back
}
//other stuff
}
该属性已被归因
pulic class Favourites
{
[Required]
public bool Cheese{get;set;}
}
不会抛出异常,因为 fav.cheese 有一个值
public int Create(Favourites fav)
{
var errors = DataAnnotationsValidationRunner.GetErrors(fav);
if (errors.Any())
throw new RulesException(errors);
//create fav
}
但是当焦点返回到控制器时 ModelState.Isvalid 显示为 false ,因此重新加载消息 值“请选择”无效。 如何阻止这种情况发生以及如何使用 xVal 驱动的“请选择值”验证方法?
Im following the approch outlined here to do this.
Im having a problem with validating a dropdown selection with xval, the MVC binding seems to infer the wrong thing an allowing the operation to pass validation, but the modelstate stores the binding exception and makes the page invalid - so I get the situation where operation is allowed to execute with invalid data, but the page is invalid so the user is lead to believe validation failed.
This might make it clearer
This is my drop down, assume the user did nothing so "Please select" is selected.
Do you like cheese:
<select name="Favourites.Cheese">
<option>Please Select</option>
<option value="1">Yes</option>
<option value="0">No</option>
</select>
<%=Html.ValidationMessage("Favourites.Cheese")%>
This is the Controller action the from posts too, MVC maps the value false to fav.cheese,
public ActionResult Create(Favourites fav)
{
try
{
_favTask.Create(fav);
}
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, "Favourites");
}
if(!ModelState.IsValid)
{
//redirect back
}
//other stuff
}
The property has been attributed
pulic class Favourites
{
[Required]
public bool Cheese{get;set;}
}
No exceptions are throw because fav.cheese has a value
public int Create(Favourites fav)
{
var errors = DataAnnotationsValidationRunner.GetErrors(fav);
if (errors.Any())
throw new RulesException(errors);
//create fav
}
But when focus returns to the controller ModelState.Isvalid comes out as false, so the from is reloaded with the message The value 'Please Select' is invalid. How do I stop this, from happening and how do I xVal driven validation method of "Please select a value"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Boolean 是一种值类型,因此它总是有一个值。 [必填]因此没有实际效果。 如果您使用 Nullable会发生什么? 反而? 如果选择了第一个选项,这应该允许 MVC 分配一个空值,然后“Required”属性可以断言用户实际上选择了一个值。
Boolean is a value type, so it will always have a value. [Required] therefore has no real effect. What happens if you use a Nullable<bool> instead? This should allow MVC to assign a null value if the first option is selected, and the Required attribute can then assert that the user did in fact select a value.