Access Javascript variable in Razor code

你离开我真会死。 提交于 2019-12-18 09:12:46

问题


I want to pass javascript variable in the C# method which is call from the Razor page. May I know how can I do that? (I refer this link but it did not work for me.)

<script type="text/javascript">
    function User(userID) {
                 var redirectUrl = '@Url.Action("AddEditUser", "User")';
                 window.location.href = redirectUrl + '/' + @SomeMethod("stringvalue",**userID**);
}

Here userID is JS object and I want to pass in my server side method. I am getting errors like "Type or namespace definition, or end-of-file expected" and ") expected".

EDITED TEXT :

My encryption code is on server side and I want to make an AJAX request with encrypted data.How can I send an encrypted data in AJAX request?


回答1:


You will need to make an ajax call and resolve the parameter server side, or create a client side equivalent of the function you are using in razor syntax. Look for ajax tutorials if you have no experience with it, i hear jQuery is very good.




回答2:


You can't do something like this, because razor works in render time of the page. UserID is not available at render time, thats why you can not pass it as a variable to servercode.

if you need to call SomeMethod on runtime, you can make a ajax request for it.



来源:https://stackoverflow.com/questions/9989993/access-javascript-variable-in-razor-code

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