带下拉菜单的 MVC 和 XVal - 值“请选择” 是无效的

发布于 2024-07-18 08:05:02 字数 1635 浏览 5 评论 0原文

我按照此处概述的方法< /a> 来执行此操作。

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有你我更好 2024-07-25 08:05:02

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.

永不分离 2024-07-25 08:05:02

如果您希望用户能够不选择一个值并推断为 false,那么我会给您的默认选择一个值 0。这样,即使他们不选择一个值,也会发送一个值,并且必需属性将满意。

<option value="0">Please Select</option>

编辑:根据您的评论,我建议使用客户端验证插件进行一些客户端验证 - 例如 jQuery 验证。 在大多数情况下,这将允许您给出合适的错误消息,而无需实际发布请求。 在服务器端,如果 ModelState 无效并且该特定字段无效,请用更合适的消息替换错误消息。 我不建议仅仅为了适应绑定框架而更改模型。

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.

<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文