locked
How come this javascript function to close the current window tab triggers the Edit method in controller? RRS feed

  • Question

  • User-1651604128 posted

    I am trying to open the separate window (tab) by using this:

    Html.ActionLink(GlobalRes.Details, "Edit",  new {id = item.Prod_ID }, new { target = "_blank" }).ToString()

    This code works fine, the separate window is opened,

    but I also want to add Close button to close it by this codes,

    <button name="button" id="CloseBtn" class="btn btn-primary btn-sm" onclick="javascript:closewindow()">@GlobalRes.Close</button>

    function closewindow() {

    window.open('', '_parent', '');

    window.close();

    }

    The above JavaScript close function works except it also triggers the [HttpPost] Edit method of controller,  it is just simple close current window, no post action at all,  and the javascript does not call controller at all,

     no sure why it also triggers the Edit method of controller, is that anything to pass the Edit method of controller?

    Thanks

    Thursday, October 31, 2019 2:37 PM

All replies

  • User-474980206 posted

    to enable the close, you are opening the current window:

      window.open('', '_parent', '');

    this of course reloads the current page, calling your controller.

    Thursday, October 31, 2019 4:54 PM
  • User-1651604128 posted

    to enable the close, you are opening the current window:

      window.open('', '_parent', '');

    this of course reloads the current page, calling your controller.

    It is good to k now that, do you know any better way, or simple way to avoid that based on what current codes I have now?

    I also tried window.open('',"_self", ""); no luck

    Thanks

    Thursday, October 31, 2019 7:46 PM
  • User-17257777 posted

    Hi Peter,

    From your description and codes, I can't reproduce your problem. Are you sure that it triggers the method when you click the colse button? or maybe somewhere else? Since you want to close the current window, why not just use window.close()?

    Best Regards,

    Jiadong Meng

    Friday, November 1, 2019 7:48 AM
  • User-1651604128 posted

    Hi Peter,

    From your description and codes, I can't reproduce your problem. Are you sure that it triggers the method when you click the colse button? or maybe somewhere else? Since you want to close the current window, why not just use window.close()?

    Best Regards,

    Jiadong Meng

    Hi Jiadong, thanks a lot for your help,

    Yes, it does trigger the Edit method of controller, I have started the debug and see the code going through, this causes the problem because I have some business log may also be triggered there.

    I have also tried to just use Window.close(), and when I click Close button, I got a extra popup window with the message of "The webpage you are viewing is trying to close the tab. Do you want to close this tab?" with Yes , No button under it. so user has to click Yes button to close it, and if user click No at this point, the form validation triggered which is also not what I wanted.

    Any idea? 

    Friday, November 1, 2019 12:47 PM
  • User-17257777 posted

    Hi Peter,

    Could you please show us the codes of the two views (the current window and the separate window) and the related controller actions such as Edit, because it works fine on my side.

    Best Regards,

    Jiadong Meng

    Monday, November 4, 2019 8:46 AM