14

How to Pass value from text using @html.actionlink in asp.net mvc3 ?

tereško
  • 58,060
  • 25
  • 98
  • 150
kiransh
  • 369
  • 2
  • 4
  • 15

4 Answers4

19

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

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • 1
    If 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
5

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

Rzv.im
  • 978
  • 7
  • 12
5

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".

Suraj Shrestha
  • 1,790
  • 1
  • 25
  • 51
-4

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);
        }
}
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51