The action is performed twice in the view.

My controller action is performed twice. Fiddler shows two requests and responses, and for the first, an icon that indicates: "The session was interrupted by the client, Fiddler or server."

But I can’t understand where this is happening, or why.

Here are the specifics:

I have a view section (ThingFinancials) that looks like this:

@{ using (Html.BeginForm("ConfirmThing", "Thing", null, FormMethod.Get, new { id = "frmGo" })) { @Html.HiddenFor(model => model.ThingID) <button id="btnGo"> Thing is a Go - Notify People</button> } } 

The javascript for btnGo looks like this:

  $("#btnGo").click(function () { var form = $("#frmGo"); form.submit(); }); 

The action (truncated) is as follows:

  public ActionResult ConfirmThing(int thingID) { [do some database stuff] [send some emails] var financials = GetFinancials(thingID); return View("ThingFinancials", financials); } 

The only thing that looks unusual to me is that the URL you see will start as [Website]/Thing/ThingFinancials/47 , and after sending the URL will be [Website]/Thing/ConfirmThing?ThingID=47 .

(If you are wondering why the Action name does not match the View name, this is because ThingFinancials has multiple form tags and they cannot have the same action name.)

Is Server.Transfer going on behind the scenes or something like that?

+4
source share
3 answers

If you use the submit button, you need to cancel the default behavior when sending using javascript, otherwise you will send it twice. Try the following:

  $("#btnGo").click(function () { var form = $("#frmGo"); // event.preventDefault(); doesn't work in IE8 so do the following instead (event.preventDefault) ? event.preventDefault() : event.returnValue = false; form.submit(); }); 
+4
source

Your int thingID is the string parameter of the request that remains with the request. At the end of the ActionResult ConfirmThing(int thingID) , everything you do returns a view. If you prefer to see a clean URL ([Website] / Thing / ThingFinancials / 47), you can make the following changes.

  public ActionResult ConfirmThing(int thingID) { [do some database stuff] [send some emails] // This logic is probably in the 'ThingFinancials' action // var financials = GetFinancials(thingID); // I'll assume we're in the same controller here return RedirectToAction("ThingFinancials", new { thingID }); } 
+1
source

This is because the jquery event just adds stopImmediatePropagation() to your jquery event.

  $("#btnGo").click(function (event){ event.stopImmediatePropagation(); }); 
0
source

Source: https://habr.com/ru/post/1392149/


All Articles