Search code examples
jqueryasp.net-mvcrazoractionlink

How to provide value from an input in ActionLink?


In my Razor page I have an input box like this. I want to provide its contents as a parameter to the action link.

<input id="FullName" placeholder="Full name" />
@Html.ActionLink("beep", "Beep", new { Info = $("#FullName").val() })

I can access the value in the input box if I execute $("#FullName").val() from the console. However, it's not accepting the syntax in the action link (expects other characters, and such).

How can I retrieve the data and send it in?


Solution

  • The simplest way I can think of is to bind your input to your action link using jQuery:

    var fullName = $("#FullName");
    //change this selector to grab your ActionLink's ID
    var anchorInfo = $("a");
    
    fullName.on("keyup", function() {
        var value = $(this).val();
        anchorInfo.attr("info", value);
        //logging the keystrokes getting to the Info attribute
        console.log(anchorInfo.attr("info"));
    });
    

    Here's a Fiddle of the straight up HTML interpretation.

    EDIT

    Fixed some bugs in the code. Should be good to go now.