1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
uxman ali
  • 43
  • 2
  • 8
  • 2
    You cannot have 2 `TextBoxFor()` for the same property - the 2nd will not have the `data-val-*` attributes necessary for client side validation. And you `@required = "required"` makes no sense (that is ignored when using mvc client side validation using `jquury.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 performance –  Mar 26 '18 at 09:31
  • if i want to add new user than i m using add user form if i want to edit user than i m using edit user form. but they both forms are in modal popup which appears when u click on button add user or edit user. – uxman ali Mar 26 '18 at 10:00
  • 1
    Use the same form! (you can either post back to a single `Edit` method, and check the `Id` value 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) –  Mar 26 '18 at 10:03
  • is there any solution for this to apply validation on that property without changing these two forms into one. – uxman ali Mar 26 '18 at 10:17
  • 1
    Why in the world would you want to duplicate all the html and have 2 forms - that is just crazy. –  Mar 26 '18 at 10:19
  • Yes, you are right. i should use one form for add and edit. Thanks – uxman ali Mar 26 '18 at 10:25

1 Answers1

2

I would suggest a restructure of the page(s) as you can't have 2 text boxes for the same property (as Stephen suggested).

I would have the following:

  • a Partial View with the common form fields (TextBoxFor etc) between add and edit.

  • a View for add (this can be a pop up) which is strongly typed to your model, includes the validatiin JS scripts etc and includes your form Partial View

  • a View for edit (again, to be used in the pop up) which is strongly typed to your model, includes the validatiin JS scripts etc and includes your form Partial View. In addition, this would have a Hidden field for the Id (or whatever your primary key is for the object)

...then, depending on what you are doing you would call the add or edit action via JS for your pop up.

scgough
  • 5,099
  • 3
  • 30
  • 48