1

I am using action link in my application.

I am having some requirement to call javascript on it's click event.

I know that I can do that using following code.

@Html.ActionLink("Home","Index","Main",new{@onclick="OnIndexCall"});

Now on the click event of action link I am computing two values in function and i also need to pass that values to the page in which I am redirecting.

I don't know how can I do that.

Please is there any way to pass the calculated value in from javascript function to redirection page?

EDIT :-

function OnIndexCall(){
    var a=10;
    var b=a+20;
}

Now i need to pass these "a" and "b" values to the redirection page.

Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
  • share your javascript code also. – Kartikeya Khosla Mar 04 '15 at 10:34
  • @Nirav Kamani please create a click event. don't try to do this as inline method, see the link for similar question http://stackoverflow.com/questions/28739962/cannot-pass-parameters-from-view-to-controller-by-using-javascript/28744490#28744490 – Frebin Francis Mar 04 '15 at 10:35
  • @FrebinFrancis Thanks but i don't want to use ajax and window.location. I only want to use actionlink nothing else.... thanks for suggestion... – Nirav Kamani Mar 04 '15 at 10:40
  • 1
    You can use jquery to reconstruct the url, adding the query string parameters. –  Mar 04 '15 at 10:41

2 Answers2

5

You can pass data through QueryString as shown :-

@Html.ActionLink("Home","Index","Main",new{ @onclick="OnIndexCall(this)" });

function OnIndexCall(elem){
    var a=10;
    var b=a+20;
    $(elem).attr('href', $(elem).attr('href') + '?a=' + a + '&b=' + b);
}

Fiddle

Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
0

Create two hidden fields in form. assign values before submiting form may be on onClick event. so you will get those values in HttpPost action.

By this way your values will not be show in URL(Query String)

HTML

<a href="/Main/Index" onclick="return OnIndexCall(this)">Home</a>
<form type="post" action="URL" id="myform">
  <input type="hidden" name="a" id="a">
  <input type="hidden" name="b" id="b">
</form>

JavaScript

function OnIndexCall(elem){
    var a=10;
    var b=a+20;
    $("#a").val(a);
    $("#b").val(b);
    $("#myform").submit();
    return false;
}
  • 1
    You cant redirect using an ajax call (ajax calls stay on the same page) –  Mar 04 '15 at 10:41