Jesun Bicar
Jesun Bicar

Reputation: 47

Javascript confirmation form submit MVC

Im trying to call the javascript confirmation form submit but it skips it. It directly calls the controller even though i want to run first the javascript for confirmation. I dont know what im doing wrong. Still new to JV Scripts.

HTML

@model WMS_Web.Models.FileMaintenance.PrincipalModels
@section Scripts {
  @Scripts.Render("~/Script/FileMaintenance/CommonFunction.js")
}
@{
  ViewBag.Title = "PrincipalModel";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Principal</h2>

@using (Html.BeginForm("Create","Principal",FormMethod.Post)) 
{
  @Html.AntiForgeryToken()
  
  <div class="form-horizontal">
      
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    
    <div class="form-group">
      @Html.LabelFor(model => model.CompanyId, htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @Html.EditorFor(model => model.CompanyId, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.CompanyId, "", new { @class = "text-danger" })
      </div>
    </div>
    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <button onclick='functionConfirm("Do you like Football?", function yes() {
          alert("Yes")
        },
        function no() {
          alert("no")
        });'>XXX</button>
    
      </div>
    </div>
  </div>
}

<div>
  @Html.ActionLink("Back", "ViewPrincipal")
</div>

Javascript

function functionConfirm(msg, myYes, myNo) {
  var confirmBox = $("#confirm");
  confirmBox.find(".message").text(msg);
  confirmBox.find(".yes,.no").unbind().click(function () {
    confirmBox.hide();
  });
  confirmBox.find(".yes").click(myYes);
  confirmBox.find(".no").click(myNo);
  confirmBox.show();
}

Upvotes: 0

Views: 1092

Answers (1)

Hossein
Hossein

Reputation: 3113

You can try this:

@using (Html.BeginForm("Create","Principal",FormMethod.Post)) 
{
   @Html.AntiForgeryToken()
   ...
   <input type="submit" name="name" value="Save" onclick="javascript: return SubmitForm();" />
} 

Javascript:

function SubmitForm() {
   var r = confirm("Are you sure you want to submit?");
    if (r == false) {
      return false;
    }
    // do something
    return true;       
}

Upvotes: 3

Related Questions