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

4

6

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?

Condensed answered 15/5, 2009 at 6:29 Comment(0)
L
2

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" />
Lorenz answered 15/5, 2009 at 7:32 Comment(4)
That almost gets me there...but this does not create a AJAX call, but rather just a standard POST.Condensed
I have changed code to make AJAX call. In case of need look also jQuery documentation to learn how to process returned ActionResult.Lorenz
Answer to this question (https://mcmap.net/q/1780371/-jquery-mvc-user-controls) could also be useful.Lorenz
ofcoarse, I think I will rather use the jquery .load method howeverPercival
C
2

Thanks a lot Alexander! Thank you for putting me on the right path. I did not try you latest code, but I was able to get your previous code working. Here is the working code. I'm sure this is all kludgy, but perhaps someone out there can show me a more elegant solution:

            <script type="text/javascript">
                $(document).ready(function() {
                    $("#myVal").change(function() {
                        changeActionURL();
                    });
                    changeActionURL();
                });
            function changeActionURL() {
                var url = '<%= new UrlHelper(ViewContext.RequestContext).Action("Action", "Controller") %>' + '?dup=' + $("#myVal").val();
                $("#u").attr('href', url);
            }
            </script>

            <a id="u" href="" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'HellaYeah' });">Report Dupe</a>

        </p>
        <div id="response">not done</div>

My solution as you can see is just to hard code the LINK instead of trying to use the ASP.NET AJAX helper class.

Condensed answered 15/5, 2009 at 15:27 Comment(0)
D
1

this is how you retrieve the value from your textbox in JQuery

var input =  $('input[name=txt_MyTextBox]').val()
Dennie answered 15/5, 2009 at 6:35 Comment(2)
Thank you, but I'm trying to get that data back to my server code...to me that means I need to pass the data back to the server via JSON or some other method. I'm HOPING asp.net has some tricky method to grab the data without having to rely on javascipt.Condensed
Since the textbox and what is in it is on the client side, you will need to rely on client side scripting to make this happen. The exception is if instead of doing the Ajax call, you submit the form, then MVC process it on the server side for you.Cylindroid
A
0

replace $.get(action, data); with $("#yourTargetId").load(action, data); you get a ajax as in the following:

<script type="text/javascript">     

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

        RunAction();
    });

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

</script>

<input type="text" id="t" />
Anticoagulant answered 22/12, 2009 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.