1

I have come across a weird issue, I hope someone can explain why the following is happening.

My controller:

MasterModel main = new MasterModel(); 

public ActionResult Index()
{
    return View();
}

[HttpGet]
public ActionResult VehicleDetail()
{
    pageSessionSetup();
    return PartialView("VehicleDetail", main.Vehicle);
}

[HttpPost]
public ActionResult VehicleDetail(VehicleDetailDisplay model)
{
    ModelState.AddModelError("", "Errors occured");
    main.Vehicle = model;
    pageSessionSetup();
    return PartialView("VehicleDetail", main.Vehicle);
}

Updated With View

My View:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

@using (Ajax.BeginForm("VehicleDetail", "Operator", null, new AjaxOptions
{
   UpdateTargetId = "VehicleDetail",
   InsertionMode = InsertionMode.Replace,
   HttpMethod = "Post"}, new { id = "VehicleDetail" }))
{
   @Html.Partial("_ValidationSummary", ViewData.ModelState)
   <div class="panel panel-default panel-body">
       ...
   </div>

   <div class ="col-lg-7 col-md-7 col-sm-7 col-xs-7 row">
       <button type="submit" value="Save" class="btn btn-lg btn-green col-lg-2 col-md-5 col-sm-7 col-xs-7">Save</button>
   </div>
}

And on my partial view I have a submit button, but when I click the submit button one time then the form gets submitted more than one time.

3
  • can you please add view ? May 26, 2016 at 5:02
  • 1
    Best guess is you have a submit button and your also submitting using ajax (and have forgotten to cancel the default submit action)
    – user3559349
    May 26, 2016 at 5:15
  • @StephenMuecke +1, but to not making us to guess, please provide your View code and Js codes (if you have).
    – SᴇM
    May 26, 2016 at 5:18

1 Answer 1

5

UPDATE:

use Html.BeginForm("ActionName", "ControllerName", FormMethod.Post) instead of Ajax.BeginForm() OR check if you have included the jquery.unobtrusive-ajax.min.js twice in your page(s)(in partial views also).

--------------------------------------------------------------------------------------------------------

@using (Html.BeginForm("ActionName", ""ControllerName", FormMethod.Post))
{    
    <label>SomeLabel</label>
    ...
    <input type="submit" value="Button" /></p> 
}
10
  • This is not necessary at all. One is marked [HttpGet] and the other [HttpPost]
    – user3559349
    May 26, 2016 at 5:13
  • Read my comment in the question :) - this code has nothing to do with a form submitting twice
    – user3559349
    May 26, 2016 at 5:16
  • I added [ActionName("VehicleDetail")] as indicated but my form is still submitting more than once.
    – ArnoDT
    May 26, 2016 at 5:40
  • @ArnoDT Please update your question with View code.
    – SᴇM
    May 26, 2016 at 5:41
  • Ajax.BeginForm() will not submit the form twice (OP still has not shown all the relevant code)
    – user3559349
    May 26, 2016 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.