1

Getting default error message for int datatype i.e he value 'abc' is not valid for age
What should i do.

   public class HomeViewModel
   {
    [Integer(ErrorMessage="provide valid numeric value")}
    public int age{ get; set; }
   }

1 Answer 1

3

in your action method that you post data to ,did you check if the model is valid ?

[HttpPost]
public ActionResult MyActionMethod(Model model)
{
  if(!ModelState.IsValid)
  {
     return View(model);
  }
  //else post data
}

if you want to display error message for the Integer you can try

[Required(ErrorMessage="Age Is Required")]
[Integer(ErrorMessage="Numbers Only")]
public int Age { get; set; }
Sign up to request clarification or add additional context in comments.

7 Comments

if there is still a prob please post your view , model and controller code
i even not able to decorete my int property with [Integer]
ensure you imported the proper namespace [Integer] public int Age { get; set; } works
on the top of your file put using DataAnnotationsExtensions;
if you put that line and there is a compile problem use nuget again and add reference because the dll is not referenced by that project
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.