0

I've been working on a HTML helper which will hide or show menu items depending on what type of user you are.

For this reason, In one of my controllers I am setting a session variables with values such as "ADMIN"

context.Session["perfil"] = "ADMIN"

The problem I am facing is that the Helper function is being called before the Javascript function which calls the controller that sets the session variables

This is how I call my HtmlHelper (through Razor)

 @using XSiteManagerWeb.Helpers
 @Html.Raw(Html.MiMenu("../Home/Configuracion", "Configuración"))

From my _Layout.cshtml

But before doing that I'm calling the function

 <script type="text/javascript">ObtenerDatosSesion();</script>

Which calls a Controler method through Ajax

...
$.ajax({
        url: "../Home/ObtenerDatosSesion",
....

Question: Why is the HtmlHelper being called before ObtenerDatosSesion(); even though I've put it before on the _Layout.cshtml ?

I've also tried calling in on window load doing this:

<body class="Fondoblue" onload="ObtenerDatosSesion();">

among other methods.

I noticed the Helper is being called before everytime after many debuggings. What I can't figure out is why that happens.

I wonder if it has anything to do with the Web.config line one has to put to use html helpers

<add namespace="XSiteManagerWeb.Helpers"/>

So to make it clear, I just want to make my "ObtenerDatosSesion(); method gets called before my html helper!

1 Answer 1

4

The razor helpers are executed server side, therefore they will be executed before any JS is rendered/executed on the page.

I would recommend moving whatever logic is in your ../Home/ObtenerDatosSesion endpoint to the same endpoint as ../Home/Configuracion. If it's going to be called more than once, you can put it in its own method.

Sign up to request clarification or add additional context in comments.

2 Comments

they will always be executed before...
That's all I needed to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.