简体   繁体   English

MVC4:在表单或摘要上显示验证错误,而不是输入?

[英]MVC4: Show validation error on the form or summary instead of an input?

I'm trying to leverage some form validation to do something it really wasn't designed to do. 我正在尝试利用某种形式验证来完成它实际上不是设计的工作。 I have a table in my form and each row has a checkbox. 我的表格中有一个表格,每行都有一个复选框。 I want to ensure that at least one of a specific type of checkbox is selected, if not I want to show a validation error. 我想确保至少选中一种特定类型的复选框,否则,我想显示验证错误。 I am doing something similar with a text box with logic that looks like this: 我正在使用带有如下所示逻辑的文本框执行类似操作:

function ValidateName() {

var $nameTextbox = $("#Name");
var $originalName = $("#OriginalName");
var nameText = $nameTextbox.val().toLowerCase();
var originalNameText = $originalName.val().toLowerCase();

//check to see if original name and group name match
if (nameText != originalNameText) {
    //This isn't the same name we started with
    if (uniqueNames.indexOf(nameText) > -1) {
        //name isn't unique, throw validation error
        var currentForm = $nameTextbox.closest("form");

        //trigger validation
        var errorArray = {};
        errorArray["Name"] = 'Name must be unique';
        currentForm.validate().showErrors(errorArray);
    }
}

} }

I've written something similar for the table and it works as long as I point the errorArray's index to the id of an input. 我为该表编写了类似内容,只要将errorArray的索引指向输入的ID,它就可以工作。 However, I want to display the error somewhere more generic like the validation summary at the top of the form. 但是,我想在更通用的位置显示错误,例如表单顶部的验证摘要。 How do I set up the error array to show on the form or the validation summary instead of a specific input? 如何设置错误数组以显示在表单或验证摘要中,而不是显示在特定输入中? Is that even possible? 那有可能吗?

One way you could do this is you set a hidden input that is false when none are check and true if 1 or more are checked. 可以执行此操作的一种方法是,设置一个隐藏输入,如果未选中任何一项,则设置为false;如果选中1个或多个,则设置为true。 You then listen to all the checkboxes by giving them all a class. 然后,您可以通过给所有课程都上一课来收听所有复选框。 I have an example shown below 我有一个例子如下所示

http://jsfiddle.net/95acw2m9/ http://jsfiddle.net/95acw2m9/

Html HTML

<input type="hidden" id="IsCheckValue" name="IsCheckedValue" value="false"/>

<input type="checkbox" class="someCheckbox"/>
<input type="checkbox" class="someCheckbox"/>
<input type="checkbox" class="someCheckbox"/>

Jquery jQuery的

  $(document).ready(function(){
    $(".someCheckbox").change(function(){
        if($(".someCheckbox:checked ").length > 0){
            $("#IsCheckValue").val("True")
        }else{
            $("#IsCheckValue").val("False")
        }
    })
})

Then pass that bool value in your model. 然后在模型中传递该布尔值。 In your controller method you can check the value of the bool. 在您的控制器方法中,您可以检查布尔值。 If the bool is false set the model to false like this 如果布尔值为假,则将模型设置为假,如下所示

ModelState.AddModelError("Checkbox", "Must select one checkbox");

You can use the @Html.ValidationSummary() to display the error in your view. 您可以使用@Html.ValidationSummary()在视图中显示错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM