Agha Hasan
Agha Hasan

Reputation: 49

I want to show a confirmation popup when form is submitted

I want to show a confirmation popup like "Are you sure you want to save?.." when a user submits the form.

Here, is the form with submit button. The form has fields but no use of showing them here.

@using (Html.BeginForm("RepPayment", "Admin", FormMethod.Post, new { @id = "Form1" }))
{ 
 <input style="box-shadow: 5px 5px 5px lightgrey;" type="Submit" value="Send" class="btn btn-info"/>
 }

and here, is the action that submits the data from the form.

[HttpPost]
    public ActionResult RepPayment(FormCollection fc)
    {
        var pm = db.tbl_Amounts.ToList();
        tbl_Amounts am = new tbl_Amounts();
        DateTime dt = DateTime.Now;

        am.fk_repId = Convert.ToInt32(fc["selectCity"]);
        am.TransferedAmount = Convert.ToInt32(fc["TransferAmount"]);
        am.TotalAmount = Convert.ToInt32(fc["Total"]);
        am.RemainingAmount = am.TotalAmount - am.TransferedAmount;
        am.Date = dt.ToShortDateString();
        db.tbl_Amounts.Add(am);
        db.SaveChanges();
         return RedirectToAction("RepPayment", "Admin");
    }

so, before saving the data in database, the user should click 'Yes' on the confirmation box to submit the form. I have tried many ways but none of them worked. Any kind of help would be appreciated. Thanks!!!

Upvotes: 0

Views: 481

Answers (1)

Ankush Jain
Ankush Jain

Reputation: 7079

A simple inline JavaScript confirm would suffice:

<form onsubmit="return confirm('Do you really want to submit the form?');">

or

In case of MVC, you can use below code

@using (Html.BeginForm("RepPayment", "Admin", FormMethod.Post, 
        new { @id = "Form1", onsubmit="return confirm('Do you really want to submit the form?');" }))
{ 
 <input style="box-shadow: 5px 5px 5px lightgrey;" type="Submit" value="Send" class="btn btn-info"/>
}

Upvotes: 2

Related Questions