Confirm with data anottation a checkbox

0

I have a form that contains 9 input of type checkbox I need to validate that when sending the form at least one of them comes tickeado.

I have the following in my view

<div class="col-12 col-sm-6 col-md-3 li">
    @Html.HiddenFor(m => m.MiCheckViewModel.Institutions[0].Value)
    @Html.HiddenFor(m => m.MiCheckViewModel.Institutions[0].Text)
    <label>
        @Html.CheckBoxFor(m => m.MiCheckViewModel.Institutions[0].Selected)
        <span class="title">@Model.MiCheckViewModel.Institutions[0].Text</span>
    </label>
</div>

In total I have 9 fields in the same way.

In my model I have the following

public class Input
{
    public Input()
    {
        this.MiSelectViewModel = new MiSelectViewModel();
        this.MiCheckViewModel = new MiCheckViewModel();
    }

    public MiSelectViewModel MiSelectViewModel { get; set; }
    public MiCheckViewModel MiCheckViewModel { get; set; }
}

public class MiSelectViewModel
{
    public List<SelectListItem> Institutions { get; set; }
    public List<int> SelectedInstitutions { get; set; }
}

public class MiCheckViewModel
{
    public List<SelectListItem> Institutions { get; set; }
    public List<int> SelectedInstitutions { get; set; }
}

How can I do this in the correct way, to be able to do the validation from the model of the fields that arrive from the view, using the data anottation of ASP.NET MVC?

The main problem is to avoid sending the form without ticking any checkbox.

    
asked by vcasas 29.06.2018 в 21:25
source

1 answer

1

You can use a script that validates the information before making the post

function myFunction() {
   var hayCheck;
   var checkBox = document.getElementById("myCheck");
   var checkBox2 = document.getElementById("myCheck2");
   var text = document.getElementById("text");
   if (checkBox.checked == true){
     haycheck=true;
   } else if(checkBox2.checked == true) {
     haycheck=true;
   }

   if(hayCkeck)
   {
     //Llamas El Post
   }else{
     //Muestras el Error
     text.html("Error")
} 
    
answered by 29.06.2018 в 23:37