如何为最小和最大数量的复选框创建 ASP.NET MVC3 验证器?

发布于 2024-10-27 15:03:24 字数 279 浏览 6 评论 0原文

我想要一个可重用的验证器,可以在一组复选框字段上使用,它可以让我指定要选择的最小数量和可以选择的最大数量。我不确定如何创建服务器端检查和客户端验证以使用不显眼的 javascript 挂钩到 jQuery 验证框架。

这个问题似乎是一个很好的开始客户端适配器,但是如何将它们结合在一起以在服务器上验证它?

I'd like to have a reusable validator that I can use on a group of checkbox fields that will let me specify a minimum number to be selected and maximum number that can be selected. I'm not sure exactly how to create both the server side check and the client side validation to hook into the jQuery validate framework using unobtrusive javascript.

This question seems to be a good start on the client side adapter, but how do you tie it all together to validate it on the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你げ笑在眉眼 2024-11-03 15:03:24

以下是您至少可以开始进行服务器端验证的方法。这是非常很好的文章,阐释了多个概念。

验证属性:

public class CheckBoxesValidationAttribute : ValidationAttribute
{
    public CheckBoxesValidationAttribute (int min, int max)
    {
        Min = min;
        Max = max;
    }

    public int Min { get; private set; }
    public int Max { get; private set; }

    public override bool IsValid(object value)
    {
        var values = value as IEnumerable<bool>;
        if (values != null)
        {
            var nbChecked = values.Where(x => x == true).Count();
            return Min <= nbChecked && nbChecked <= Max;
        }
        return base.IsValid(value);
    }
}

模型:

public class MyViewModel
{
    [CheckBoxesValidation(1, 2, ErrorMessage = "Please select at least one and at most 2 checkboxes")]
    public IEnumerable<bool> Values { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Values = new[] { true, false, true, false }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

视图(~/Views/Home/Index.cshtml):

@Html.ValidationSummary()
@using (Html.BeginForm()) 
{
    @Html.EditorFor(x => x.Values)
    <input type="submit" value="OK" />
}

编辑器模板(~/Views/Home/EditorTemplates/bool.cshtml):

@model bool
@Html.CheckBoxFor(x => x)

Here's how you could start at least for server side validation. Here's a very nice article that illustrates multiple concepts.

Validation attribute:

public class CheckBoxesValidationAttribute : ValidationAttribute
{
    public CheckBoxesValidationAttribute (int min, int max)
    {
        Min = min;
        Max = max;
    }

    public int Min { get; private set; }
    public int Max { get; private set; }

    public override bool IsValid(object value)
    {
        var values = value as IEnumerable<bool>;
        if (values != null)
        {
            var nbChecked = values.Where(x => x == true).Count();
            return Min <= nbChecked && nbChecked <= Max;
        }
        return base.IsValid(value);
    }
}

Model:

public class MyViewModel
{
    [CheckBoxesValidation(1, 2, ErrorMessage = "Please select at least one and at most 2 checkboxes")]
    public IEnumerable<bool> Values { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Values = new[] { true, false, true, false }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

View (~/Views/Home/Index.cshtml):

@Html.ValidationSummary()
@using (Html.BeginForm()) 
{
    @Html.EditorFor(x => x.Values)
    <input type="submit" value="OK" />
}

Editor template (~/Views/Home/EditorTemplates/bool.cshtml):

@model bool
@Html.CheckBoxFor(x => x)
对你而言 2024-11-03 15:03:24

Brad Wilson 在 mvcConf 上做了精彩的演讲关于mvc中的验证。

Brad Wilson had great presentation on mvcConf about validation in mvc.

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