Display alert when loading page

Asked

Viewed 2,059 times

1

I’m using Asp.net-mvc and as far as I know the Onload event would work if I put in body, which is in ~Layout, but I need this alert to be displayed in a single page, because if I put in body will run every time... how can I do?

code of the View:

@model Tribus.Models.Pagamento

@{
    var Id = Session["PacoteID"];
}

<script type="text/javascript">
    $(document).ready(function () {
        //window.onload = window.alert("entrou aqui");

        window.onload = function () {
            var id = $(this).attr(Id);//id do item

            window.alert(id);
            var url = '@Url.Action("RetornoRegistrar", "Pagamento")';
            $.post(url, { id: id },
            function (data) {
                window.alert("Registrado com Sucesso\nLogo seu produto será liberado.")
            });
        });
    });
</script>
  • Can you explain better "displayed on a single page"? you mean on a specific page or only once for each user?

  • This would be a page that the user would pass, during a process and when he enters this screen, the alert is fired at him. I just wanted to know how to call the function when loading this page.

  • @Sergio put the page code

1 answer

1


Use the Razor Rendersection...

It serves to create a section in the Layout for you to specify in the Views that implement where the code will be docked when rendered. You can use for scripting or css purposes.

It consists of two main parameters. The first is the name and the second is required.

@Rendersection("nomeSection", required: false)

NOTE: Note that the second parameter is set to "false", but set to true if you want to set the mandatory Section statement for each View that implements the layout.

In your situation you will have to do as follows:

In _Layout.cshtml add Rendersection to the end of the body (since it is a script and it is recommended to stay at the end of the body)

<!DOCTYPE html>
<html>
<head>
    ...
</head>
<body>
    ...

    @RenderSection("scripts", required: false)

</body>
</html>

You will implement the Section in the following way:

@Section setName{ "code you will implement" }

In your code:

@model Tribus.Models.Pagamento

@{
    var Id = Session["PacoteID"];
}

@section scripts{

<script type="text/javascript">
    $(document).ready(function () {
        //window.onload = window.alert("entrou aqui");

        window.onload = function () {
            var id = $(this).attr(Id);//id do item

            window.alert(id);
            var url = '@Url.Action("RetornoRegistrar", "Pagamento")';
            $.post(url, { id: id },
            function (data) {
                window.alert("Registrado com Sucesso\nLogo seu produto será liberado.")
            });
        });
    });
</script>

}

Then remove the comment from //window.onload = window.Alert("entered here"); and make sure everything works correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.