Get textbox value for Ajax ActionLink in MVC 4 / Razor

Anders picture Anders · Mar 23, 2013 · Viewed 11.5k times · Source

I am doing a classic search field with a button. I am using Ajax.ActionLink for the button, and I cannot figure out how to get the textbox value posted in the ActionLink. I looks like this:

<div class="input-append">
    <input type="text" id="Company_CompanyName" />
    @Ajax.ActionLink("search",
                     "CompanySearch",
                      new { searchString = "???" },
                            new AjaxOptions
                            {
                                HttpMethod = "GET",
                                InsertionMode = InsertionMode.Replace,
                                UpdateTargetId = "CompanySearchResults"
                            },
                            htmlAttributes: new { @class = "btn" })
</div>

<div id="CompanySearchResults"></div>

Where the ??? is, is where I don't know how to get the value from the textbox in. What do I do?

UPDATE 1: It is a nested form

As I should have mentioned in the original post/question, this is a nested form, i.e. there is an outer form to be submitted. Therefore if I make a Ajax.BeginForm() with a submit inside it, it will invoke the submission of the outer form. I obviously want to avoid that.

Answer

monkeyhouse picture monkeyhouse · Mar 24, 2013

This answer is moot b/c the link is wihin a form, I'll try to make another answer in a bit...

Use an ajax form for this - not a link. You need a form to include values in the postback, ie.

@using (Ajax.BeginForm("search", "CompanySearch",new AjaxOptions { HttpMethod = "GET", InsertionMode =      InsertionMode.Replace, UpdateTargetId = "SearchResults" }))
{    
<h1>Search</h1>
<input type="text" placeholder="Search Terms" name="SearchTerms"/>
 <input type="submit" name="Command" value="Search" />
}


<div id="SearchResults"></div>