Request ajax accessing scripts already included

0

Hey guys,

I have a view that renders a partial view.

Index.cshtml

<script type="text/javascript" src="~/Scripts/jquery-2.1.1.js"></script>

    <div class="tab-control" data-role="tab-control">
        <ul class="tabs">
            <li class="active">@Ajax.ActionLink(Resources.Base.Entry, "_List", "Entrada", null, new AjaxOptions() { UpdateTargetId = "_Content", InsertionMode = InsertionMode.Replace, LoadingElementId = "Div_Loading" })</li>
            <li>@Ajax.ActionLink(Resources.Base.Data, "_Edit", "Entrada", null, new AjaxOptions { UpdateTargetId = "_Content", InsertionMode = InsertionMode.Replace, LoadingElementId = "Div_Loading" }, new { id = "Dados" })</li>
        </ul>

        <div class="frames" id="_Content">
            @{Html.RenderPartial("_List");}
        </div>
    </div>

_List.cshtml

$(document).ready(function () {
    $.get("@Url.Action("_Grid", "Data")", function (data) {
        $('#con').replaceWith(data);
    });
});

This view I do the jQuery include in it however in the partial view with this code above it says that jQuery does not exist. If I re-add the jquery over the code quoted above it works normal.

Is there a way to make all of my ajax requests after the load of the page have access to the included scripts?

All of them are being loaded at the bottom of the page (before the "")

Vlw!

Off: I noticed that there are some questions in Stackoverflow that are worth reputation, how can I ask a question like this?

    
asked by anonymous 30.07.2014 / 23:03

2 answers

0

@Cigano thanks for the help.

I solved the problem by putting the scripts in the head instead of putting them before closing the body tag.

As I've read in several surveys and benchmarks when putting the scripts at the bottom of the page, the scripts load in parallel, it's no use if you need to do some gambiarras to get access to scripts or to load again in ajax requests. >

If the system has significant use of ajax, I recommend that you always load the scripts at the top of the page, otherwise load before the body closes so the visual part of the page is displayed faster even if the scrits have not yet been loaded

There are several techniques to improve performance and loading time, among them cache, minification, bundles, etc ...

    
01.08.2014 / 15:08
1

In ASP.NET MVC, Scripts should not be placed in Partial Views for the simple fact that Partial Views can be reused in various parts of your code, causing strange behavior of the Scripts.

The correct thing is to put your scripts in @session scripts of your Index.cshtml :

@section scripts {
    <script type="text/javascript">
        $(document).ready(function () {
            $.get("@Url.Action("_Grid", "Data")", function (data) {
                $('#con').replaceWith(data);
            });
        });
    </script>
}

In View Shared/_Layout.cshtml , you should have the following statement:

@RenderSection("scripts", required: false)

This is where the contents of @section scripts will be rendered.

    
30.07.2014 / 23:15