简体   繁体   中英

Scripts for PartialView MVC3 Not Loading

I am using ASP MVC3 for which i am using a partial view but the issue is am not able to load the scripts into the same. Below is an code for which i have tried:

<script type="text/javascript">
    $('#bodytag').load(function () {
        alert('Load was performed.');
    });
</script>
<div class="acc-expand-bkg" id="bodytag">
    <div class="submenu_dropdown clearfix">
        <div class="filter_bar clearfix">
            <div class="left" style="margin-left: 10px;">
                <label style="width: auto; margin-top: 7px;">
                    Standard:</label>
                @Html.DropDownList("teamDetailId", ViewBag.TeamNames as SelectList, null, new { id = "teamDetailId" })
            </div>
        </div>
    </div>
</div>

The above partial view is not getting loaded with the script. I tried with document.ready neither it worked for it. Can you please tell me the work around for the same.

Thank you in advance

Try like this:

<script type="text/javascript">
    alert('Load was performed.');
</script>

But normally scripts should not be placed inside partial views. They should not be mixed with markup. Normally scripts belong to separate javascript files. So a better approach would be to define a javascript function in a separate file:

function doSomething() {
    alert('Load was performed.');
}

and then invoke this javascript function once you load the partial view using AJAX:

$.ajax({
    url: 'some url',
    type: 'post',
    success: function(result) {
        $('#someId').html(result);
        // the partial view was injected into the DOM => now we could
        // invoke our function:
        doSomething();
    }
});

The load function is used to load content from server using ajax and update the inner content of the specified html element. You have to pass the server url as the first argument to the method.

$('#bodytag').load('ajax/test.html', function() {
  alert('Load was performed.');
});

You have to put the script block below the html.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM