问题
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