Search code examples
asp.net-mvc-4html.actionlink

Passing a GET parameter to ActionLink in ASP.NET


Sorry but I am new to C# and ASP.NET and I saw alot of posts about this problem but I quite didn't get it. I am trying to understand how to pass a GET parameter to an action thru HTML.ActionLink:

here is the the URL:

http://localhost:36896/Movies/SearchIndex?searchString=the

and my CSHTML page should look like this:

<input type="Text" id="searchString" name="searchString" />
@Html.ActionLink("Search Existing", "SearchIndex", new { searchString = "the"}) 

this hard coded parameter "the" is actually working, but how can I select the input element with id=searchString, with something like document.getElementById("searchString").value

Thanks,


Solution

  • This makes the @Html.EditorFor refer to the Title field of the object, kinda in a random way but it works!

    @using (Html.BeginForm ("SearchIndex", "Movies", FormMethod.Get))
    {
        @Html.EditorFor( x => x.ElementAt(0).Title)
        <button type="submit">Search</button>
    }
    

    Still couldn't pass input parameter to the URL in the GET.

    EDIT:

    FINAL SOLUTION:

    @Html.TextBox("SearchString")
        <button type="submit">Filter</button>
    

    and on the controller side, switch the input parameter. Basically it will automatically recognize the passed parameter.

    public ActionResult SearchIndex(string searchString)
            {
               ...
            }