How to Pass textbox value using @html.actionlink in asp.net mvc 3

后端 未结 4 2034
盖世英雄少女心
盖世英雄少女心 2020-12-11 01:55

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

相关标签:
4条回答
  • 2020-12-11 02:14

    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

    0 讨论(0)
  • 2020-12-11 02:15

    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);
            }
    }
    
    0 讨论(0)
  • 2020-12-11 02:33

    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

    0 讨论(0)
  • 2020-12-11 02:34

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

    0 讨论(0)
提交回复
热议问题