asp.net mvc validation must be a number custom error

前端 未结 10 1150
时光取名叫无心
时光取名叫无心 2021-02-06 01:14

I am new to asp.net and I have a problem. When the users insert in a editor for a decimal field something other than numbers, they get an error \"Field name\" is not a number. B

10条回答
  •  感情败类
    2021-02-06 01:44

    Unfortunately this is is not a trivial task. However you can try the following hack... Better to do this only on essential fields, as this is more code to maintain.

    In the controller's action method

    if(ModelState.IsValid)
    {
      // code
    }
    else
    {
      if (ModelState["YourField"].Errors.Count > 0)
      {
        ModelState["YourField"].Errors.Clear(); 
        ModelState.AddModelError("YourField", "Your custom message here");
      }
    
      // code
    }
    

提交回复
热议问题