How to Pass value from text using @html.actionlink in asp.net mvc3 ?
4 Answers
None of the answers here really work. The accepted answer doesn't refresh the page as a normal action link would; the rest simply don't work at all or want you to abandon your question as stated and quit using ActionLink
.
MVC3/4
You can use the htmlAttributes
of the ActionLink
method to do what you want:
Html.ActionLink("My Link Title", "MyAction", "MyController", null, new { onclick = "this.href += '&myRouteValueName=' + document.getElementById('myHtmlInputElementId').value;" })
MVC5
The following error has been reported, but I have not verified it:
A potentially dangerous Request.Path value was detected

- 20,682
- 14
- 97
- 107
-
1If you get the dangerous Request.Path error in MVC5 or above this is due to enhanced security as & is not permitted in the URL by default. You can add the below to your web.config file which will allow it (anytihing in the list is not allowed): `
` – guytz72 Jul 04 '19 at 02:11
to pass data from the client to the server you could use a html form:
@using (Html.BeginForm(actionName,controllerName)) {
<input type="text" name="myText"/>
<input type="submit" value="Check your value!">
}
be sure to catch your myText variable inside your controller's method

- 978
- 7
- 12
-
Should've included how you do that: `string myText = Request.Form["myText"].Value;` – vapcguy Sep 21 '18 at 18:45
Rather than passing your value using @Html.actionlink, try jquery to pass your textbox value to the controller as:
$(function () {
$('form').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: { search: $('#textboxid').val()},
success: function (result) {
$('#mydiv').html(result);
}
});
return false;
});
});
This code will post your textbox value to the controller and returns the output result which will be loaded in the div "mydiv".

- 1,790
- 1
- 25
- 51
-
The page doesn't refresh and needed to write an other method to refresh to refresh – pavan kumar Jul 14 '20 at 19:38
you can use this code (YourValue = TextBox.Text)
Html.ActionLink("Test", new { controller = "YourController", action = "YourAction", new { id = YourValue }, null );
public class YourController : Controller
{
public ActionResult YourAction(int id)
{
return View("here your value", id);
}
}

- 28,516
- 5
- 46
- 51
-
try with <%=Html.TextBoxFor(x => x.Property, new { id = "IdTextBox" })%> – Aghilas Yakoub Jul 03 '12 at 08:26
-
@Html.TextBoxFor(x => x.name, new { id = "IdTextBox" }) @Html.ActionLink("Test", "Create","Home",new{id=IdTextBox}),but it didn't work.:( – kiransh Jul 03 '12 at 11:31