문제

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,

도움이 되었습니까?

해결책 2

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)
        {
           ...
        }

다른 팁

If the value you want to send as GET parameter is not known on the server you cannot use the Html.ActionLink helper to add it. You need to use javascript to manipulate the existing link and append the parameter.

It looks like you have an input field that contains a search string and you want to send the value entered in this field to the server. A better way to handle this scenario is to use an HTML form with method="GET" instead of an ActionLink. This way you don't need to use any javascript - it's part of the HTML specification:

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

Now when you click on the Search button the value entered in the SearchString field will automatically be sent to the SearchIndex action:

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

But if you absolutely insist on using an ActionLink you will have to write javascript to manipulate the href of the existing link when this link is clicked in order to append the value to the url. It's an approach I wouldn't recommend though because the HTML specification already provides you this functionality throughout HTML forms.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top