I am using ASP.NET MVC for form validation. I have one page of "Users" which have two forms named as "adduser" form and "edituser" form.I'm trying to apply same validation message against m.user.firstName property for both forms.
Add user form:
<form id="addUserForm" method="post" class="form-horizontal">
@Html.TextBoxFor(m => m.user.FirstName, new { @class = "form-control-modal", @id = "fNameEditVal" })
@Html.ValidationMessageFor(m => m.user.FirstName, "")
</form>
Edit user form:
<form id="editUserForm" method="post" class="form-horizontal">
@Html.TextBoxFor(m => m.user.FirstName, new { @class = "form-control-modal", @placeholder = "", @id = "fNameAddVal", @required = "required" })
@Html.ValidationMessageFor(m => m.user.FirstName, "")
</form>
But when I submit the form of add user without writing any text in that field it properly shows the validation message "Please enter your first name " but when I submit the edit user form it will not show any message. When I change @Html.TextBoxFor to @Html.EditorFor, the message is showing up perfectly.
TextBoxFor()for the same property - the 2nd will not have thedata-val-*attributes necessary for client side validation. And you@required = "required"makes no sense (that is ignored when using mvc client side validation usingjquury.validate. But your view makes no sense. You can only submit one form so what is the point of 2 forms - all you doing is degrading performanceEditmethod, and check theIdvalue to determine if its a new user or existing user, of you could conditionally change the forms action attribute based on which button was clicked)