@Html.ValidationMessageFor() required field displaying message when populated
@Html.ValidationMessageFor() required field displaying message when populated
I am currently using ASP.net MVC (VB).
I have a model with the following property:
<Required(ErrorMessage:="Contact name is required")>
Public Property contact_name() As String
<Required(ErrorMessage:="Contact name is required")>
Public Property contact_name() As String
In my controller method (accepts a post request and binds various parameters to the model) I set contact_name from two other parameters :
model.contact_name = first_name + " " + last_name
model.contact_name = first_name + " " + last_name
I return to my view containing a form which has an input field with a value of model.contact_name
and I use the tag
model.contact_name
@Html.ValidationMessageFor(Function(model) model.contact_name
@Html.ValidationMessageFor(Function(model) model.contact_name
The problem is that the error message is always displayed, unless the POST request has the parameter: contact_name
. Even if the error message is displayed, if I submit the form the model is valid. (modelState.isValid == true
).
contact_name
modelState.isValid == true
Since I set the value of model.contact_name and pass the model to the view, technically, the model state should be valid, the required property contact_name is not null or blank and the validationMessageFor the contact_name property should not be appearing.
Why it is appearing?
How do I stop it from appearing if the value of model.contact_name is not blank?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.