Partial View does not load Javascript - Asp.Net MVC

Asked

Viewed 1,736 times

0

I’m switching to a Partial View via a two-parameter controller, music and artist. Already in PV I pass to the function fetchLetra() that is in the file letraAPI.js these parameters for the return of Function, however, this is not happening because apparently Partial is not loading any script, it is this way

@model letra.Infra.Models.Letra



@{
    var artista = Model.Artista.ToString();
    var musica = Model.Musica.ToString();
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="@Url.Content("~/js/letraAPI.js")"></script>

<script type="text/javascript">
    fetchLetra(artista, musica);
    alert("OK");
</script>

Loading the page the HTML looks like this

    <div id="letraAPI" class="panel-content">



    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="/js/vagalumeAPI.js"></script>

    <script type="text/javascript">
        fetchLetra(artista, musica);
        alert("OK");
    </script>
</div>

nothing happens, not even Alert runs.

  • fetchLetra is what? A function in the file letraAPI.js?

  • Exactly jbueno!

  • This is to be executed as soon as the screen is loaded? Did not miss to put it inside $(document).ready(function () { });?

  • No, this is running after a click action, which calls the controller and the controller passes the required parameters to Partial

  • Perai, you put JS code inside the partial?

  • The references are in Partial because they are in main and still not working, so I thought I had to put in both places. One detail is that by placing this function directly in the main referencing the file letraAPI.js, it works normally.

Show 1 more comment

2 answers

1

You see, a View (whether Partial or not) serves to dynamically mount an HTML page on the server side,

then any code written within @{ and } is processed on the server side (included tags HTML, these will also be processed by the server) and is not available on the client side. what will be sent to the customer is the result of this processing, ie a HTML, JavaScript or CSS valid.

@model letra.Infra.Models.Letra
@{
    var artista = Model.Artista.ToString();
    var musica = Model.Musica.ToString();
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="@Url.Content("~/js/letraAPI.js")"></script>

<script type="text/javascript">
    var artista = @artista;
    var musica = @musica;
    fetchLetra(artista, musica);
    alert("OK");
</script>

in the above example, the View will mount a script dynamically using the values artista and musica, but remember, that this script will be executed only on cliente (which is probably a Browser).

0

If you do it the way below, it works, but you don’t need to put the script in partial, if you’re calling it ajax, then you can call the methods by callback "complete:".

<script>
    $(function () {
        fetchLetra(artista, musica);
        alert("OK");
    });
</script>

Browser other questions tagged

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