14

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

4 Answers 4

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

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

1 Comment

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): <httpRuntime requestPathInvalidCharacters=",*,%,:,\,?" />
6

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

1 Comment

Should've included how you do that: string myText = Request.Form["myText"].Value;
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".

1 Comment

The page doesn't refresh and needed to write an other method to refresh to refresh
-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);
        }
}

2 Comments

try with <%=Html.TextBoxFor(x => x.Property, new { id = "IdTextBox" })%>
@Html.TextBoxFor(x => x.name, new { id = "IdTextBox" }) @Html.ActionLink("Test", "Create","Home",new{id=IdTextBox}),but it didn't work.:(

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.