0

In View-

<input type="text" hidden="hidden" id= "nameString" name="nameString" value="xyz" />
@Html.ActionLink("Save", "HomePage", "ControllerName", new { nameString = "/* Value from above input here.*/" })

In this case I want to pass "xyz" through this hyperlink.

Can't use Get or POST methods to pass this value.

This HomePage view is not the current ActionLink view either, so can't grab the value in Controller using Request.Form["nameString"];

I tried JQuery like following but its not working-

var nameVar = document.getElementById('nameString').value;
$.ajax({
                type: 'GET',
                url: "@Url.Action("HomePage", "ControllerName")",
                data: { nameString : nameVar }                   
            });

I checked in debugger and saw that Controller is actually getting value and processing it but nothing is coming on browser. I am not sure how ajax works.

2
  • the only way to put a value from the view into a link is by creating the url with jquery and redirecting. If you want to do an ajax call instead it sounds like everything works. nothing coming to the browser would be what you pass from the controller and what you set up in the success part of the ajax call Commented Jan 27, 2015 at 22:28
  • I am new to JQuery, Can you please show some code snippet to creating the url with jquery and redirecting to HomePage. Thank you. Commented Jan 27, 2015 at 22:40

2 Answers 2

1

since the redirect will happen in jquery you don't need a helper

<input type="button" class="btnRedirect" value="Click Here" />

then in your script

$('.btnRedirect').on('click', function(){
    var url = '@Url.Action("HomePage", "ControllerName", new { textValue = "----" })'.replace("----", $('#nameString').val());
    window.location = url;
});
Sign up to request clarification or add additional context in comments.

Comments

0

This is how it would be done with ajax although this will not handle returning a page.

var name = $("#nameString").value;
$.get("/ControllerName/HomePage",{ nameString : nameVar })
           .done(function(){
                 window.location.assign("/ControllerName/HomePage");
                 });

2 Comments

Yes Rajdeep, it's not handling a returning page. But I don't want it to handle it either. I just want it to redirect to HomePage, where I have written the logic to deal with the data. Can you please suggest how to redirect to that HomePage. I want it to function like simple hyperlink. Thanks.
There is a little bit problem somewhere in the call. Its calling controller 2 times. First call, it passes correct value in nameString. But in second call, controller is receiving null value. Thanks for the help and code snippet. We got the solution for original problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.