MattNorman
Well-known member
- Joined
- May 22, 2021
- Messages
- 98
- Programming Experience
- 1-3
I have a bootstrap modal within my main layout view that is used for displaying error messages.
Whenever there is an error I take the following action:
This works fine in most cases however I have an issue with the modal not showing in specific cases.
I have a 'Manage' view that has a data table with each row having a delete action link.
Clicking the action link shows a confirmation modal (part of the Manage view not the layout).
Clicking the confirm button on the modal triggers a post action to delete the record.
Within the post method, I carry out some validation to check if the record can be deleted. If not, I add some error data to the session and redirect back to the manage view.
The manage view is displayed and using a breakpoint, I can see that the viewbag check in the layout view is being called. The modal does not get displayed however.
Here is the post method for deleting the item:
Line 19 is where the redirect occurs and is working as expected. I can even see in the main layout that the error data is present in the view bag. It just does not display the modal for some reason.
Whenever there is an error I take the following action:
- Add error data to session
- Redirect to relevant action
- At the start of action, get error data from session and add to view bag
C#:
@if (ViewBag.ModalMessage is not null)
{
<script type="text/javascript">
window.onload = function () {
$("#MyPopup").modal("show");
};
</script>
}
This works fine in most cases however I have an issue with the modal not showing in specific cases.
I have a 'Manage' view that has a data table with each row having a delete action link.
Clicking the action link shows a confirmation modal (part of the Manage view not the layout).
Clicking the confirm button on the modal triggers a post action to delete the record.
Within the post method, I carry out some validation to check if the record can be deleted. If not, I add some error data to the session and redirect back to the manage view.
The manage view is displayed and using a breakpoint, I can see that the viewbag check in the layout view is being called. The modal does not get displayed however.
Here is the post method for deleting the item:
C#:
[HttpPost]
[TypeFilter(typeof(LoginControl))]
[TypeFilter(typeof(ManageAccountTypes))]
public ActionResult Delete(int id)
{
string method_name = "Delete(int id)";
try
{
// Load popup data in to view bag.
UpdatePopupMessage();
// Check if account type is used.
bool in_use = _blAccount.GetAccountCountByAccountTypeID(id) > 0;
if (in_use)
{
_sessionManager.AddViewPopup(ViewPopups.AccountTypeDeleteInUse);
return RedirectToAction("Manage", "AccountTypes");
}
// Delete from database and get result.
bool result = _blAccountType.DeleteAccountType(id);
// Check result.
if (!result)
{
_sessionManager.AddViewPopup(ViewPopups.AccountTypeDeleteFailed);
}
// Redirect to manage users action.
return RedirectToAction("Manage", "AccountTypes");
}
catch (Exception ex)
{
_blErrorLog.AddErrorLog(new()
{
LogDate = DateTime.Now,
Username = _sessionManager.GetUserSession()!.Username ?? "",
IPAddress = _sessionManager.GetClientIPAddress() ?? "",
Namespace = $"{_classNamespace}.{method_name}",
ErrorMessage = ex.ToString(),
Data = $""
});
_sessionManager.AddViewPopup(ViewPopups.GeneralError);
return RedirectToAction("Manage", "AccountTypes");
}
}
Line 19 is where the redirect occurs and is working as expected. I can even see in the main layout that the error data is present in the view bag. It just does not display the modal for some reason.