I have a Razor PartialView that works with Metro-UI-style tiles. It's a very simple PartialView that allows me to display tiles bound to a ViewModel
, really nothing spectacular.
Since it's not used in all pages, I don't want to include the jQuery block on every page load. Instead I prefer that the script is declared inline with the PartialView, but registered once in the pagecode.
The view is the following (I have reenginered Metrofy template)
@model IEnumerable<MetroStyleTile>
<div class="container tiles_hub">
<div class="sixteen columns alpha">
@foreach (MetroStyleTile tile in Model)
{
<div class="tile_item four columns alpha">
<a href="@Url.Action(tile.Action, tile.Controller, routeValues: tile.MvcArea != null ? new { area = tile.MvcArea } : null)" class="tile">
@Html.Label(tile.Description)
@if (tile.ImageUrl != null)
{
@Html.Image(tile.ImageUrl.ToString(), tile.Title, htmlAttributes: new { @class = "tile_img" })
}
</a>
</div>
}
</div>
@section Scripts{
<script type="text/javascript">
$(document).ready(function ($) {
//Tiles hover animation
$('.tile').each(function () {
var $span = $(this).children('span');
$span.css('bottom', "-" + $span.outerHeight() + 'px');
});
var bottom = 0;
$('.tile').hover(function () {
var $span = $(this).children('span');
if (!$span.data('bottom')) $span.data('bottom', $span.css('bottom'));
$span.stop().animate({ 'bottom': 0 }, 250);
}, function () {
var $span = $(this).children('span');
$span.stop().animate({ 'bottom': $span.data('bottom') }, 250);
});
});
</script>
}
</div>
The above code is fine when I use the PartialView only once in the page, but if I use the @Html.Partial
twice I get the script twice.
Now let me clarify: I often ask question on StackOverflow to learn more from the platform, and I don't usually like workaround solutions.
I could use a Javascript global variable to see if the function must be declared again or not.
I could register the script in the Layout to make it appear in all pages and not worry.
I'm asking how to print the script to Response
only once. Learning how to do this allows me to lighten page payload when multiple PartialViews are developed like this (so the client gets the script only when needed, and only once if needed).
How can I do that?