MVC3/4 Validating Hidden Fields

前端 未结 2 653
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  死守一世寂寞
    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);
        }
    

提交回复
热议问题