6

Here is my scenario

@Html.Textbox("value")

how to pass above text box value to below action link

@Html.ActionLink("Search","Search",new {firstname=value)
2
  • Where do you want to do this? Client side? After posting to the server? Have you tried anything that hasn't worked? Commented May 9, 2013 at 10:30
  • in controller ,server side Commented May 9, 2013 at 10:34

2 Answers 2

13

You can do it using javascript. First generate the anchor tag with a href having a faked value of firstname:

<a href="@Url.Action("Search", "Controller", new {firstname="xxxx"}") id="lnk">Search</a>

Also, generate the with an ID (i.e. txtSearch).

Then, using javascript you can attach the click event of this . Using jQuery code will be something like:

$("#lnk").click(function(evt) {
    var fakedUri = $("#lnk").prop("href");
    var uri = fakedUri.replace("xxxx", $("#txtSearch").val());
});

Greetings!

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, I'm trying to use your code, but I'm getting the XXXX in my controller, not the value of the textbox. is there anything I'm missing?
Ok I got the solution. I just needed to add uri as lnk's href prop again. $("#lnk").prop("href", uri); has done the trick.
@Jain you just saved me from hours of wondering why it didn't work. Also, .prop() was not available in my version of jquery, I had to use .attr() for anyone else with this issue. Thanks
@eiximenis i have 5 parameters then how to pass it ??
Just use same pattern: <a href="@Url.Action("action","controller", new {p1="xxx", p2="yyy"}" id=lnk">...</a> var uri = fakedUri.replace("xxx", valueOfParam1).replace("yyy", valueOfParam2)
1

You need to use a form

<form method="post" action="@Url.Action("Search", "Search")">
     @Html.Textbox("value")
</form>

2 Comments

is it possible to with out using form, because we are planned to implement search functionality on index page...
yes, you can use Javascript to perform a POST to the url. jQuery AJAX is useful for this - api.jquery.com/jQuery.ajax

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.