How can I pass a Textboxes value to my Ajax.ActionLink?

Whozumommy picture Whozumommy · May 15, 2009 · Viewed 12.7k times · Source

In my ASP.NET MVC application I want a user to add a value into a textbox and then press my Ajax.ActionLink. I want to do something like this:

Ajax.ActionLink("Go", "Action", "Controller", new { value = textbox1.value })

Or how else can I get this textbox value back to my action? Jquery?

Answer

Alexander Prokofyev picture Alexander Prokofyev · May 15, 2009

You may run action using AJAX $.get method:

<script type="text/javascript">     

    $(document).ready(function()
    {
        $("#t").change(function()
        {
            RunAction();
        });

        RunAction();
    });

    function RunAction()
    {
        var action = '<%= Url.Action("Action", "Controller") %>';
        var data = $("#t").serialize();
        $.get(action, data);
    }

</script>

<input type="text" id="t" />