How to send Textbox value to ActionLink in asp.net

2020-07-13 08:39发布

问题:

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)

回答1:

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!



回答2:

You need to use a form

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