Using a javascript variable within Razor

不问归期 提交于 2021-01-29 07:30:37

问题


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:


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



来源:https://stackoverflow.com/questions/33957996/using-a-javascript-variable-within-razor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!