0

I have an inherited MVC5 web application using Razor that when the page loads, there are two drop downs displayed, some data, and five buttons, one of which is disabled (Save). When I select a different option from the first drop down, what is supposed to happen an input field with a date picker is to display, along with an "OK" button, and the "Save" button is supposed to be enabled. This works fine in IE 11, but in Microsoft Edge, a message appears asking "Leave site? Changes you made may not be saved." If I click the "Leave" button, the spinner appears and I stay on the page.

In IE 11, when any other option is selected from the first dropdown, the spinner appears, the page reloads with the input field with date picker, but OK button appears, and the Save button is enabled. The only time a similar message displays is when the back button is clicked.

I went through the code and found the following:

The code for the first drop down:

@Html.DropDownListFor(m => m.StatusId, new SelectList(Model.StatusList, "Value", "Text", Model.StatusId), new { @class = "form-control", @id = "StatusList"})

I also found a submit button that has been hidden:

@Html.HiddenFor(m => m.CurrentTypeName, htmlAttributes: new { id = "CurrentTypeName" })
<input id="FinalizeValidationButton" type="submit" name="action:FinalizeValidation" style="visibility:hidden" />

In a javascript file I found the following:

//This is the first dropdown mentioned above
 $('#StatusList').change(
function () {
    triggerFinalizeValidation();
    setVisibility();
});

var triggerFinalizeValidation = function () {
var selectedStatus = $('#StatusList').val();
$("#CurrentSignificantActivityTypeName").val($("div.active").prop("id"));
if (selectedStatus != 1) {
    navParams.userAction = true;
    $('#finalizeButton').prop('disabled', true);
    navParams.isDirty = false;
    $('#FinalizeValidationButton').trigger('click');
}};

So what's happening is, the user selects a different option from the first dropdown (StatusList), which triggers $('#StatusList').change, which in turn calls triggerFinalizeValidation(), which in turn adds a click event to the hidden submit button ("FinalizeValidationButton"). For whatever reason, it's like it triggers a postback like response, which causes Microsoft Edge to think the user is trying to leave the current page, when they are not.

The hidden button, when clicked, executes the following code in the RiskController.ca. There are a number of dropdowns, fields, etc on the screen other than the fields/buttons that have already been mentioned. So the this part of the code validates what the user has selected and peforms calculations based on what was selected. When I comment out the click event in the JavaScript, the browser message doesn't display, but then the FinalizeValidation method doesn't execute either.

    [AcceptVerbs(HttpVerbs.Post)]
    [MultiButtonAction(Name = "action", Argument = "FinalizeValidation")]
    public ActionResult FinalizeValidation(RiskViewModel viewModel)
    {
        string currentActivityTypeName = viewModel.CurrentActivityTypeName;
        var risk = _riskRepository.FindFull(viewModel.Id);

I'm wondering if there is a way to use @onchange in the dropdown to somehow call the FinalizeValidation method in the controller to see if that resolves the browser message from appearing...Thoughts?

user2073183
  • 141
  • 1
  • 3
  • 13
  • From the code you provide, I can't locate the issue. There're many values that are not being defined. Could you please provide more detailed code snippet which we can make a test and **reproduce the issue**? For example, you can provide more related Model, Controller code, etc. – Yu Zhou Nov 24 '21 at 09:54
  • @YuZhou I've done more looking into the code and have narrowed down what I think is the cause so have revised my original issue. Thanks! – user2073183 Nov 25 '21 at 23:45
  • I made a sample to test using your code logic but there's no "Leave site" pop up showing in Edge. I searched related information and found that the message is [beforeunload](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) event. I added this code `window.onbeforeunload = () => '';` in JavaScript and it will show the "Leave site" message in Edge. Could you please check if you have used any `beforeunload` event in JavaScript? – Yu Zhou Nov 26 '21 at 08:26
  • @YuZhou I saw reference to that in my research and searched the entire solution on "beforeunload" and the only reference I could find was in the jQuery min file: parent.attachEvent( "onbeforeunload", function() and i&&i.attachEvent&&i!==i.top&&i.attachEvent("onbeforeunload". – user2073183 Nov 26 '21 at 16:16
  • @YuZhou I tried adding window.onbeforeunload = () => ''; to the JavaScript before the click event $('#FinalizeValidationButton').trigger('click');. It doesn't like the '=>' – user2073183 Nov 26 '21 at 16:25
  • @YuZhou I added window.addEventListener('beforeunload', function (e) { e.preventDefault(); e.returnValue = ''; }); to the JavaScript file (same file as the click event, etc, but the "Leave now?" message still displayed. – user2073183 Nov 26 '21 at 16:34
  • Which version of Edge are you using? [Arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) `=>` should work in Edge. Could you please try to add `window.onbeforeunload = null;` in click event to disable the beforeunload event to see if it works? You can also refer to [this similar thread](https://stackoverflow.com/questions/40425682/disable-changes-you-made-may-not-be-saved-pop-up-window/40426206#40426206). – Yu Zhou Nov 29 '21 at 10:05
  • @YuZhou I'm using version 90.0.818.51. It wasn't Edge that was having an issue. It was Visual Studio. I did try window.onbeforeunload = null before trying what I posted below. That didn't work either. I appreciate your time and assistance with this issue! – user2073183 Nov 30 '21 at 14:51

1 Answers1

0

I ended up using

$(window).off('beforeunload');

After

$('#FinalizeValidationButton').trigger('click');

Which resolved the issue.

user2073183
  • 141
  • 1
  • 3
  • 13
  • Thanks for posting the solution for this issue. You can mark your answer as an accepted answer. It can help other community members in future with similar kinds of issues. Thanks for your understanding. – Yu Zhou Nov 30 '21 at 01:40