In my VIew ive got 3 checkboxes, i want to validate them that way :
if one checkbox is checked other 2 is disabled, if checked checkbox is unchecked, other 2 checkboes is enabled again.
Controller
public ActionResult SolarPart()
{
var model = new SolarParentViewModel();
var list = new List<URLTimeLimitViewModel>();
list.Add(new URLTimeLimitViewModel { Name = "14 dage", IsChecked = false, Id = 1 });
list.Add(new URLTimeLimitViewModel { Name = "1 Måned", IsChecked = false, Id = 2 });
list.Add(new URLTimeLimitViewModel { Name = "2 Måneder", IsChecked = false, Id = 3 });
model.TimeLimit = list;
return View(model);
}
View
@for (var i = 0; i < Model.TimeLimit.Count; i++)
{
<div class="editor-label">
@Html.LabelFor(c=>Model.TimeLimit[i].Name, Model.TimeLimit[i].Name)
@Html.HiddenFor(c=>Model.TimeLimit[i].Id)
@Html.CheckBoxFor(c=>Model.TimeLimit[i].IsChecked)
</div>
}
If the values are mutually exclusive you might consider using radio buttons instead of checkboxes. They seem more adapted to your scenario. If for some very weird reason you still want to use checkboxes which are mutually exclusives you could use javascript and subscribe to the change event of each of them and based on the value toggle the other 2 checkboxes. And for validating this model on the server you could write a custom validation attribute which could either be applied to the
TimeLimitproperty on your view model:and then: