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
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
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
@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
).
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?