MVC3/4 Validating Hidden Fields

前端 未结 2 640
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 03:53

I have set up model validation for my form but validation doesn\'t seem to work at all. I don\'t suppose anybody can help. I\'ve tried using the below work-around but that k

相关标签:
2条回答
  • Its too late to change the ignore on the validator in this way after the unobtrusive script. This is because the validator only pulls in the defaults once - when it is created. The unobtrusive script creates the validator for you. You need to reference the existing validator object and update it.

    try this

    <script>
        $(document).ready(function () {            
            $('form').validate().settings.ignore = []
        });
    </script>
    
    0 讨论(0)
  • 2020-12-20 04:49

    If you don't have to use javascript, in your Controller, and in your action of the related view, you can add a model error before validating your model. Example:

     [HttpPost]
            public ActionResult Fix(YourModel mdl)
        {
            if (mdl.Customer_ID>Int32.MaxValue || mdl.Customer_ID<1)
                ModelState.AddModelError("", "Your error message!");
    
            if (ModelState.IsValid)
            {
    
    
               //
               //Some code
               //
    
                return View("YourView", yourlist);
            }
    
            return View(mdl);
        }
    
    0 讨论(0)
提交回复
热议问题