【问题标题】:Validate that checkboxes are checked when not using model验证在不使用模型时检查复选框
【发布时间】:2012-08-08 00:36:38
【问题描述】:

在用户可以转到下一页之前,我需要检查一些复选框。显示验证消息的最佳方式是什么?

查看:

@Html.CheckBox("terms_eligibility")
@Html.CheckBox("terms_accurate")
@Html.CheckBox("terms_identity_release")
@Html.CheckBox("terms_score_release")

控制器:

[HttpPost]
public ActionResult Review(ReviewModel model)
{
    // Make sure all Terms & Conditions checkboxes are checked
    var termsEligibility = Request.Form.GetValues("terms_eligibility")[0];
    var termsAccurate = Request.Form.GetValues("terms_accurate")[0];
    var termsIdentityRelease = Request.Form.GetValues("terms_identity_release")[0];
    var termsScoreRelease = Request.Form.GetValues("terms_score_release")[0];

    if (termsEligibility == "true" && termsAccurate == "true" &&
        termsIdentityRelease == "true" && termsScoreRelease == "true")
    {
        return RedirectToAction("CheckOut","Financial");
    }

    return null;
}

编辑,

我进行了建议的更改。现在如何让同一页面显示错误消息?

我把模型中的属性改成这个

 [RequiredToBeTrue(ErrorMessage = "*")]

这是控制器

[HttpPost]
public ActionResult Review(ReviewModel model)
{


    if(ModelState.IsValid)
    {
        return RedirectToAction("CheckOut", "Financial");
    }

    return RedirectToAction("Review");
}

【问题讨论】:

    标签: c# asp.net-mvc-3 checkbox validation


    【解决方案1】:

    我建议您使用视图模型和自定义验证属性:

    public class RequiredToBeTrueAttribute : RequiredAttribute
    {
        public override bool IsValid(object value)
        {
            return value != null && (bool)value;
        }
    }
    

    和视图模型:

    public class MyViewModel
    {
        [RequiredToBeTrue]
        public bool TermsEligibility { get; set; }
    
        [RequiredToBeTrue]
        public bool TermsAccurate { get; set; }
    
        [RequiredToBeTrue]
        public bool TermsIdentityRelease { get; set; }
    
        [RequiredToBeTrue]
        public bool TermsScoreRelease { get; set; }
    
        ... some other properties that are used in your view
    }
    

    视图当然会被强类型化到视图模型中:

    @model MyViewModel
    
    @Html.ValidationSummary(false)
    @using (Html.BeginForm())
    {
        @Html.CheckBoxFor(x => x.TermsEligibility)   
        @Html.CheckBoxFor(x => x.TermsAccurate)   
        @Html.CheckBoxFor(x => x.TermsIdentityRelease)   
        @Html.CheckBoxFor(x => x.TermsScoreRelease)   
        ...
        <button type="submit">OK</button>
    }
    

    最后你的控制器动作将把视图模型作为参数:

    [HttpPost]
    public ActionResult Review(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // there were validation errors => redisplay the same view  
            // so that the user could fix them
            return View(model);
        }
    
        // at this stage we know that validation succeeded =>
        // we could proceed in processing the data and redirecting
        ...
    
        return RedirectToAction("CheckOut", "Financial");
    }
    

    请注意,Request.Form.GetValues("terms_eligibility")[0];termsEligibility == "true" 这样的代码实际上并不是您希望在正确编写的应用程序中看到的那种代码。

    【讨论】:

    • 感谢您的帮助。如果验证失败,如何显示错误消息。
    • 这就是我的示例中的 @Html.ValidationSummary(false) 助手所做的。但是,如果您想为每个单独的复选框执行此操作,您还可以在每个复选框旁边使用 ValidationMessageFor 助手。例如:@Html.ValidationMessageFor(x =&gt; x.TermsEligibility).
    • 谢谢我越来越近了。有两个问题 1. 当验证失败时,摘要确实显示错误,但整个页面显示没有任何 CSS 样式..它的所有文本都是黑白的。
    • 2.当它通过验证时,它会挂在同一页面上的部分视图上,而在该页面的原始 GET 上它没有问题。
    • sorry #2 不正确,当我注释掉 paritals 并且验证失败时,它会显示正确的验证但没有样式。如果它通过验证,它工作正常。
    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 2017-04-03
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多