2

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?

alok_dida
  • 1,723
  • 2
  • 17
  • 36

2 Answers2

2

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.

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
  • My main requirement is to encrypt the Query params and than send a request to the server.So it is not good to add this method in each and every page. Is it good to add property in MODEL which contains encrypted data or to make an Ajax request is better (considering perfomance) – alok_dida Apr 03 '12 at 09:24
  • Where do you want to win performance , client side or server side ? If you want to encrypt to store it in the database you probably want to encrypt it on the server, if you want to encrypt it to send it securely to the server you want to use SSL. – Willem D'Haeseleer Apr 03 '12 at 09:29
1

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.

Özgür Kara
  • 1,333
  • 10
  • 22
  • My main requirement is to encrypt the Query params and than send a request to the server. Is it good to add property in MODEL which contains encrypted data or to make an Ajax request is better (considering perfomance)? – alok_dida Apr 03 '12 at 09:24
  • For better performance you can generate a action witch will take UserID and other params and redirects to your action, and call this action in javascript. so you will get your result with one request. – Özgür Kara Apr 03 '12 at 11:33
  • But the thing is that, I am making an ajax request and which will make data visible to user and I want to restrict to see UserID and other params. I have edited my question relevant to my purpose. – alok_dida Apr 03 '12 at 11:58