C# 验证未使用模型时是否选中复选框

C# 验证未使用模型时是否选中复选框,c#,asp.net-mvc-3,checkbox,validation,C#,Asp.net Mvc 3,Checkbox,Validation,在用户进入下一页之前,我需要选中一些复选框。显示验证消息的最佳方式是什么 视图: 控制器: [HttpPost] public ActionResult Review(ReviewModel model) { // Make sure all Terms & Conditions checkboxes are checked var termsEligibility = Request.Form.GetValues("terms_eligibility")[0];

在用户进入下一页之前,我需要选中一些复选框。显示验证消息的最佳方式是什么

视图:

控制器:

[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");
}

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

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>
}
当然,视图将被强类型化为视图模型:

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

注意,
Request.Form.GetValues(“terms_合格性”)[0]
termsEligibility==“true”
确实不是您希望在正确编写的应用程序中看到的那种代码。

谢谢您的帮助。如果验证失败,如何显示错误消息。在我的示例中,
@Html.ValidationSummary(false)
助手就是这样做的。但是,如果要对每个复选框执行此操作,还可以使用每个复选框旁边的
ValidationMessageFor
helper。例如:
@Html.ValidationMessageFor(x=>x.TermsEligibility)
。谢谢,我越来越近了。有两个问题1。当验证失败时,摘要确实会显示错误,但整个页面显示时没有任何CSS样式。所有文本都是黑白的。当它通过验证时,它会挂起在同一页面上的部分视图上,而它在该页面的原始GET上没有问题。抱歉#2是不正确的,当我注释掉paritals并且验证失败时,它会显示正确的验证,但没有样式。如果它通过了验证,就可以正常工作。