user3818229
user3818229

Reputation: 1647

Js file not loaded after partial view is refreshed

I have the simple partial view:

<div class="well">
    <h3>
        <strong>@Model.Name</strong>
        <span class="pull-right label label- primary">@Model.AverageRaiting.ToString("# stars")</span>
    </h3>
    <span class="lead">@Model.Description</span>
    @Html.DialogFormLink("Update", Url.Action("UpdatePhoto", new {id = @Model.PhotoId}), "Update Photo", Url.Action("Photo"))
    @Html.Action("InitializeAlerts")
</div>
@section scripts{
    <script src="~/Scripts/reload.alerts.js"></script>
}

Everything is ok by the first time. But after I replace the partial view by another one like above the custom script "reload.alerts.js" doesn't work. This script is for @Html.Action("InitializeAlerts") action. InitializeAlerts method just return a partial view named _Alert. What shall I do to fix this problem? Thanks.

Upvotes: 2

Views: 2148

Answers (1)

teo van kot
teo van kot

Reputation: 12491

You shouldn't use @section scripts in Partial Views.

Everything works fine if you render it in Main View becouse Razor knows where to put your script on _Layout but when you refresh if you basically make ajax call and you get row html.

Since you get only Partial page without _Layout Razor just cut this line so your script link doesn't even came to client.

You have 2 options:

  1. like @Alorika said you should place your script in main View. But there could be problems and it depends on what your script do and how it do it. That's good practice and i recommend to use it. If script doesn't work you should look at it where it falls.
  2. Delete @section scripts and put your script link without it. I'm not sure, but i suspect that there is some possibilities when your script won't work this way either depending on how you write it.

Upvotes: 4

Related Questions