Partial View does not load Javascript - Asp.Net MVC

0

I'm moving to a Partial View through a two parameter controller, music and artist. Now in the PV I pass to the function fetchLetra () that is in the file letterIj.js these parameters for the return of the 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><scripttype="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><scripttype="text/javascript" src="/js/vagalumeAPI.js"></script>

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

Nothing happens, not even the alert is executed.

    
asked by anonymous 18.11.2016 / 17:19

2 answers

1

Understand, a View (whether it's Partial or not) serves to dynamically mount an HTML page on the server side,

Then all code written within @{ and } is processed on the server side (including tags HTML , these will also be processed by the server) and is not available on the client side. what will be sent to the client 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><scripttype="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 artista and musica values, but remember, this script will only execute on cliente probably is a Browser ).

    
04.06.2017 / 20:49
0

If you do this, it works, but you do not need to put the script in the partial, if you are calling it by ajax, you can call the methods by the "complete:" callback.

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