Reputation: 3660
My form has a reset button that returns a shiny new View. However, all of the fields which are invalid at the time the reset button is pressed show their corresponding validation messages for a split second before the page refreshes with the new View. It looks unprofessional.
On the client side (in JavaScript), how do I post and disregard validation?
If my user intends to reset the form, I do not want to flash red validation messages at him. I am using typical MVC Data Annotations like the following in my Model.
[Display(Name="IP Address")]
[Required(ErrorMessage = "Required")]
public string address { get; set; }
Here is a relevant portion of my View. All of the form fields are declared in similar fashion.
<p>
@Html.LabelFor(c => c.address):
@Html.TextBoxFor(c => c.address)
@Html.ValidationMessageFor(c => c.address, String.Empty, new { @class ="errorMessage"})
</p>
Finally, here is the Javascript that runs when the reset button is clicked. I think that submit() is what causes all of the validation messages to appear, but I do not sure how to post any other way.
$('#resetButton').click(function () {
this.form.submit();
});
My attempts to Google "javascript POST without validating" keeps finding pages like "validate without post", and if you try the phrase "POST without validating" in quotes you get almost nothing useful.
Upvotes: 0
Views: 79
Reputation: 6366
You could use
[ValidateInput(false)]
attibute and assign it to your controller's action if you are sure that this action will not require any validation.
The question is whether you really need to make a post call, when you only need to reset your form (so no server action is needed). In case it is just about resetting the form you could use the following:
$('#resetButton').click(function () {
this.form.reset();
});
Instead of:
$('#resetButton').click(function () {
this.form.submit();
});
Upvotes: 1
Reputation: 2851
Don't submit a POST
at all; reset the form in place. Use the DOM (non-jquery) form reset function:
$('#resetButton').click(function () {
this.form.reset();
});
Upvotes: 1