0

I am aware that Razor is server side and Javascript client side. I am trying to workaround this. Here's what I wanted:

RAZOR:

if (count > 0)
{
    t.Add().Text("Yeah")
         ...
         ...
    }
}

But that count is defined at the beginning of the View, like this:

@{
    int count = 2;
}

But I can't define it like that: the value for count is on a javascript variable, set on document.ready. Then, I want to use it in the Razor if condition.

Is it possible to workaround this?

1 Answer 1

0

Simply No , The reason is same server-side language run first, than after browser run client side languages, So you can use server side variables values in client side language, you can generate client side code from server side but this all will not work in vice-versa,

The option you have for your problem is only Ajax request

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

4 Comments

Yeah, I am aware of that but I am looking to work around that. An Ajax request you say? How would that work? How could I have the count variable populated that way?
Try like this count = @:js_variable
@:js_variable is just outputting 'js_variable' in HTML. @chiapa you have to clarify and extend your solution with additional code in order for us to fully understand your intention here. You could try to add the 'count' value somewhere in HTML as the hidden field, something like this: <input type="hidden" value="@count" /> and then read this filed in you JS if statement. This way, you will be able to read it in JS, but I think that there will still be an issue with the server side razor code being executed first. Give it a try, and let us know what happened.
@MarkoJ., I had tried that and the variable is empty, exactly because of what you said, Razor gets executed first

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.