MVC-5 Razor Validation Errors showing on page load

Viewed 3457

I have an insert page when I go to the insert page all the validation field is being shown,

[Required(ErrorMessage ="Please Enter Name")]
        public string ccname { get; set; }

This is my class where I declared string ccname with required validation message

ENTER NAME

and it supposed to appear when user clicks on insert without entering data in ccname

but validatation message is being shown on the page load

@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname)

I tried few things but nothing works,

here is an example

in my controller I added ModelState.clear();

public ActionResult insert()
{
    ModelState.Clear();
    return View();
}

and in my view I changed the code from

@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname)

to

@Html.TextBoxFor(model => model.ccname, new { @class = "textboxstyle" })
@Html.ValidationMessageFor(model => model.ccname,"",new {@style= ".validation-summary-valid { display:none; }" })

but neither of these works

what should I do now?

1 Answers
Related