0

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>
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • fom the function definition, its clear that you are calling the submit and clear function at once, so make a callback on form submit and on the callback clear the form, or better use ajax – Akhil Aravind Feb 29 '20 at 12:18
  • are you checked [this answer](https://stackoverflow.com/questions/14589193/clearing-my-form-inputs-after-submission) – Mohammed Sajid Feb 29 '20 at 15:15

0 Answers0