Reputation: 2362
I am working with MVC 5.
I need to call a function before every page is loaded.
I did it in the _Layout.cshtml
view:
$(function () {
$('body').on('click', function (e) {
var valor = GetSession();
$('#hdnSessionTime').val(valor);
});
}
I save a value in a hidden field defined in the layout page, and every time the user click on every page, the function called GetSession
executes.
The problem is that it execute after @RenderBody()
and I need it before...
Is that possible?
Upvotes: 1
Views: 2092
Reputation: 9927
Sometimes you want to perform logic either before an action method is called or after an action method runs. To support this, ASP.NET MVC provides filters
. Filters are custom classes that provide both a declarative and programmatic means to add pre-action and post-action behavior to controller action methods. You can use custom filter
like this.
Client Side
If you want to run code in client side, you can use this code (before @RenderBody()):
<script type="text/javascript">
$(document).ready(function () {
var valor = GetSession();
$('#hdnSessionTime').val(valor);
});
</script>
see this for similar example.
Upvotes: 1