This is a question of the branch " Js file that does not load after updating partial viewing ." The main problem is that if I put my script in the main view, this does not work in partial. My custom script:
$(function() { $.ajaxSetup({ cache: false }); var timer = window.setTimeout(function () { $(".alert").fadeTo(1000).slideUp(1000, function () { $(this).hide(); }); }, 3000); $("[data-hide]").on("click", function () { if (timer != null) { clearTimeout(timer); $(this).closest("." + $(this).attr("data-hide")).hide(); } }); });
Partial view of the photo where I need to use my script:
<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>
And viewing the partail "_Alert" to be passed to the partial, where I need to use the script above:
@{ var alerts = TempData.ContainsKey(Alert.TempDataKey) ? (List<Alert>)TempData[Alert.TempDataKey] : new List<Alert>(); if (alerts.Any()) { <hr /> } foreach (var alert in alerts) { var dismissableClass = alert.Dismissable? "alert-dismissable" : null; <div class="alert alert-@alert.AlertStyle fade in @dismissableClass"> @if (alert.Dismissable) { <button type="button" class="close" aria-label="close" data-hide="alert">×</button> } @Html.Raw(alert.Message) </div> } }
source share