Im following the approch outlined here to do this.
我遵循这里概述的approch来做到这一点。
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.
我在使用xval验证下拉选择时遇到问题,MVC绑定似乎推断出允许操作通过验证的错误,但是模型状态存储了绑定异常并使页面无效 - 所以我得到的操作是允许使用无效数据执行,但页面无效,因此用户会认为验证失败。
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,
这也是来自帖子的Controller动作,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
}
The property has been attributed
该物业已被归因
pulic class Favourites
{
[Required]
public bool Cheese{get;set;}
}
No exceptions are throw because fav.cheese has a value
没有例外,因为fav.cheese有价值
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"?
但是当焦点返回到控制器时,ModelState.Isvalid显示为false,因此从重新加载了消息“请选择”值无效。如何阻止这种情况发生?如何xVal驱动验证方法“请选择一个值”?
2 个解决方案
#1
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.
Boolean是值类型,因此它始终具有值。 [必需]因此没有实际效果。如果您使用Nullable
#2
If you want the user to be able to not select a value and infer false, then I would give your default selection a value of 0. This way there will be a value sent even if they don't choose one and the Required attribute will be satisfied.
如果您希望用户不能选择一个值并推断为false,那么我会给您的默认选择值为0.这样就会发送一个值,即使他们没有选择一个值,并且必需属性将满意。
<option value="0">Please Select</option>
EDIT: Based on your comments, I suggest some client-side validation using a client-side validation plugin -- like jQuery validation. This will allow you, in most cases, to give a suitable error message without having to actually post the request. On the server side, if the ModelState is invalid and that particular field is invalid, replace the error message with a more appropriate one. I wouldn't suggest changing the model just to accommodate binding framework.
编辑:根据您的意见,我建议使用客户端验证插件进行一些客户端验证 - 比如jQuery验证。在大多数情况下,这将允许您提供合适的错误消息,而无需实际发布请求。在服务器端,如果ModelState无效且该特定字段无效,请将错误消息替换为更合适的错误消息。我不建议改变模型只是为了适应绑定框架。
#1
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.
Boolean是值类型,因此它始终具有值。 [必需]因此没有实际效果。如果您使用Nullable
#2
If you want the user to be able to not select a value and infer false, then I would give your default selection a value of 0. This way there will be a value sent even if they don't choose one and the Required attribute will be satisfied.
如果您希望用户不能选择一个值并推断为false,那么我会给您的默认选择值为0.这样就会发送一个值,即使他们没有选择一个值,并且必需属性将满意。
<option value="0">Please Select</option>
EDIT: Based on your comments, I suggest some client-side validation using a client-side validation plugin -- like jQuery validation. This will allow you, in most cases, to give a suitable error message without having to actually post the request. On the server side, if the ModelState is invalid and that particular field is invalid, replace the error message with a more appropriate one. I wouldn't suggest changing the model just to accommodate binding framework.
编辑:根据您的意见,我建议使用客户端验证插件进行一些客户端验证 - 比如jQuery验证。在大多数情况下,这将允许您提供合适的错误消息,而无需实际发布请求。在服务器端,如果ModelState无效且该特定字段无效,请将错误消息替换为更合适的错误消息。我不建议改变模型只是为了适应绑定框架。