I am using asp.net mvc and I am trying to enter data into a form then submit the values to the database. I would the form to be cleared after data has been submitted to the database but unfortunately it seems that the form is being cleared before the submission.
Here is my form
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "applicationForm" ,@name= "applicationForm", @class = "applicationForm" }))
{
@Html.AntiForgeryToken()
<div class="container">
<div class="master">
@*<h2>Order</h2>*@
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<table class="table table-responsive">
<tr>
<td>
First Name
</td>
<td>
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { id = "firstName", placeholder = "Enter first name", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
Middle Name
</td>
<td>
@Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { id = "middleName", placeholder = "Enter middle name", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { id = "lastName", placeholder = "Enter last name", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</td>
</tr>
</table>
<div>
<input type="submit" value="Submit" id="submitButton" class="btn btn-warning" onclick = "submitForm()" />
</div>
</div>
</div>
}
Here is my script at the top of the html form which contains the form
<script>
;
function submitForm() {
var frm = document.getElementsByName('applicationForm')[0];
frm.submit(); // Submit the form
frm.reset(); // Reset all form data
// return false; // Prevent page refresh
}
</script>