0

Im having issues when placing html content from partial view into modal content via ajax, not all events defined in _layout.cshtml are being attached to elements in modal content.

Setup:

_Layout.cshtml

<!DOCTYPE html>
<html lang="en">

<head>

//custom css is here.

</head>

<body>

@RenderBody()

<partial name="_Modals" /> <-- all variations of modals exist in this file

//custom javascript files <-- **this is failing to attached to modal content** 

@RenderSection("CustomScriptsSection", false);

</body>

</html>

_CustomLayout.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<partial name="_SideBar" />

<main>

    <partial name="_NavBar" />

    <div>

        @RenderBody()

        <partial name="_Footer" />

    </div>

</main>

@section CustomScriptsSection{

    @RenderSection("Scripts", false)
}

_Viewstart.cshtml

@{
    Layout = "~/Views/Shared/_CustomLayout.cshtml";
}

The Ajax Call snippet from a standard razor view(not a page).

@section Scripts{

        $('#DefaultModelBtn').on('click', function (e) {

            var RegisterURLPartial = "/Home/RegisterPartial"

            $('#modal-default').modal('show');

            $('#modal-default').off('shown.bs.modal').on('shown.bs.modal', function () {

                if ($('#PartialContainer').length) {

                    $('#PartialContainer').load(RegisterURLPartial, function (response, status, xhr) {


                    });
                }
            });

        });
}

Controller Method for the Partial Page


public virtual IActionResult RegisterPartial() =>  PartialView("~/Views/Home/Partial/_Register.cshtml");

The partial Page itself (ambiguous Action, Id, and Controller names)


<form id="someId" asp-action="someAction" asp-controller="someController" method="post">
    <div class="input-group input-group-outline mb-3">
        <label asp-for="Name" class="form-label"></label>
        <input asp-for="Name" type="text" class="form-control" autocomplete="off">
    </div>

    <div class="input-group input-group-outline mb-3">
        <label asp-for="Email" class="form-label"></label>
        <input asp-for="Email" type="email" class="form-control" autocomplete="off">
    </div>
    <div class="input-group input-group-outline mb-3">
        <label asp-for="Password" class="form-label"></label>
        <input asp-for="Password" type="password" class="form-control" autocomplete="off">
    </div>
    <div class="text-center">
        <button type="submit" class="btn btn-lg bg-gradient-primary btn-lg w-100 mt-4 mb-0">Sign Up</button>
    </div>
</form>

What I noticed was the css is being formatted properly, but the javascript that attaches to those css class are not working, eg. focus event is missing in the modal. (input-group-outline css class is missing focus event)

No Razor Pages are used at all (@page not used).

Thanks for the help guys.

2
  • Hi @sage reddy, did you mean you custom js in _Layout.cshtml but the partial view does not trigger that js? Commented Sep 12, 2022 at 7:17
  • Yes, @Rena the custom scripts are in the _layout, when the ajax call to the partial replaces the html in the modal body, the events for that javascript from my custom js file is not attaching to the ajax html. Commented Sep 12, 2022 at 13:52

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.