0

I have a view (eg. Index.cshtml) and in page model (eg. Index.cshtml.cs). There's one javascript function in view that I want to call from OnPost() method in page model. So I tried using ScriptManager but it says that "ScriptManager does not exists in current context". Clicking "Show potential fixes" doesn't show any solution to solve this error.

How to solve this or is there any other alternative way to call javascript function from page model?

My Javascript in View:

<script type="text/javascript">
    function openModal() {
        $('#myModal').modal('show');
    }
</script>

My code to call the javascript function using ScriptManager in Page Model:

public ActionResult OnPost(){
    ........//Some codes
    ........//Some codes

    ScriptManager.RegisterStartupScript(this,this.GetType(),"Pop", "openModal();", true);
}

Appreciate any help and suggestions. Thanks in advance!

1 Answer 1

1

As far as I know, the ScriptManager.RegisterStartupScript method is used to the Asp.Net application, by using it with the ScriptManager control and the UpdatePanel control, we could achieve the partial view refresh. But it is not apply to the Asp.net core Razor application. So, it will show the "ScriptManager does not exists in current context" error.

According to your code, it seems that you want to show the popup model, and want to achieve the partial view refresh. If that is the case, you could try to use the Bootstrap Model to show the popup model, and use partial view/page to show the modal content.

Check this tutorial: Razor Pages And Bootstrap - Modal Master Details

Besides, you could also search "using jquery ajax popup modal in asp net core razor pages" to find more tutorials.

Sign up to request clarification or add additional context in comments.

Comments

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.