Question

I would like to validate that at least one of three fields is not empty. I am trying to do this using ASP.NET remote validation mechanism.

I have the following model:

public class MyModel
    {
        public MyModel()
        {
            EmployeeIds = new List<int>();
            ManagerIds= new List<int>();
        }

        [Remote("AtLeastOneSelected", "Company", AdditionalFields = "EmployeeIds, ManagerIds")]
        public int? SuperManagerId { get; set; }

        public IEnumerable<int> EmployeeIds { get; set; }

        public IEnumerable<int> ManagerIds{ get; set; }

    }

Controller:

public ActionResult AtLeastOneSelected(int supermanagerid, IEnumerable<int> employeeids, IEnumerable<int> managerids)
        {
            var isSuperManagerSelected = Request.Params["SuperManagerId"] != "null";
            var isEmployeeSelected = Request.Params["EmployeeIds"] != "null";
            var isManagerSelected = Request.Params["ManagerIds"] != "null";
            return Json(isSuperManagerSelected || isEmployeeSelected || isManagerSelected, JsonRequestBehavior.AllowGet);
        }

EmployeeIds and ManagerIds are implemented on the view using multiselect. It was validated once when I modified SuperManagerId first time. And when I try to modify it again or even submit my form, remote validation doesn't work.

I tried to force validation on the view:

$('body').on('click', '#create-company', function () {
    $("form").validate().form();
    if ($("form").valid()) {
        alert('valid');
        $("form").submit();
    } else {
        alert('error');
    }
});

but it didn't help.

Was it helpful?

Solution

I found the problem. The result of the first validation was cached. Attribute [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] helped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top