How to Pass textbox value using @html.actionlink i

2019-01-19 15:44发布

问题:

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

回答1:

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



回答2:

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



回答3:

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



回答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);
        }
}