Display Alert Loading Page

1

I'm using asp.net-mvc and as far as I know the OnLoad event would work if I put it in body, which is in ~ Layout, but I need this alert to be displayed on a single page, because if I put in body it will run all instead ... how can I do it?

View code:

@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>
    
asked by anonymous 20.11.2016 / 20:40

1 answer

1

Use Razor's 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 it for scripting or css purposes.

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

@RenderSection ("nameSection", required: false)

NOTE: Note that the second parameter is set to "false", but set to true if you want to define the required section declaration for each View that implements the layout.

In your situation you will have to do the following:

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

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

    @RenderSection("scripts", required: false)

</body>
</html>

You will implement Section in the following way:

@section nameSection {"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.

    
20.11.2016 / 22:37