@Model.Name

Js file not downloaded after partial viewing is updated

I have a 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> } 

For the first time, everything is in order. But after I replaced the partial view with another like the one above, the custom script "reload.alerts.js" does not work. This script is for @Html.Action("InitializeAlerts") action. InitializeAlerts simply returns a partial view named _Alert. What should I do to fix this problem? Thanks.

+2
source share
1 answer

You should not use @section scripts in partial views.

Everything works fine if you do it in Main View, because Razor knows where to place the script on _Layout , but when you update, if you basically make an ajax call and you get the html string.

Since you only get a partial page without _Layout , Razor just shortens this line, so your script link doesn't even come to the client.

You have 2 options:

  • as @Alorika said you should place your script in the main view. But there can be problems, and it depends on what your script does and how it does it. This is a good practice, and I recommend using it. If the script does not work, you should look at it where it falls.
  • Remove @section scripts and put the script link without it. I'm not sure, but I suspect that there are some possibilities when your script will not work the way it depends on how you write it.
+4
source

Source: https://habr.com/ru/post/1239123/


All Articles