alok_dida
alok_dida

Reputation: 1733

Access Javascript variable in Razor code

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?

Upvotes: 2

Views: 6032

Answers (2)

&#214;zg&#252;r Kara
&#214;zg&#252;r Kara

Reputation: 1333

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.

Upvotes: 1

Willem D&#39;Haeseleer
Willem D&#39;Haeseleer

Reputation: 20190

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.

Upvotes: 2

Related Questions