0

In VB, I am working on an MVC project. I have a text box to enter some search criteria, and a button next to it that upon pressing will submit the value of the text box. Html Snippet:

<input type="text" id="mySearchField" /> 
<button onclick="location.href='@Url.Action("Search", "Movies", New   RouteValueDictionary(New With {.searchCriteria = document.getElementById('mySearchField') }))'">Search</button>

My question revolves around the final part:

 New RouteValueDictionary(New With {.searchCriteria = document.getElementById('mySearchField') }))'"

The code above does not work or compile, but this is my general idea of what I am attemping to do.

I want to pass the value of the text box along into the Movies/Search function, however I am at a loss as to how to format the line to mix the html, asp, and javascript all at once.

My VB Function for clarity:

    Function Search(searchCriteria As String) As ActionResult

        Return View()
    End Function

Any advice is much appreciated!

1 Answer 1

1

This should be pretty close. This has jQuery as a prerequisite. To make this maintainable (you can do this, or not), I'd use NewtonSoft.Json to serialize the URL to Javascript properly, and url-encode the text box value.

<input type="text" id="mySearchField" /> 
<button id=myButton>Search</button>

<script>
     var url = '@Html.Raw(Url.Action("Search", "Movies"))';
     $("#myButton").click(function(){
         location.href = url + '?searchCriteria=' + $("#mySearchField").val()
     });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you codenoir for the response. I am sorry for the delay. I still have not gotten a chance to test this, but I wanted to express my appreciation in the mean time.
Finally got a chance to test, and it works perfectly! Once jQuery is added, this works like a charm. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.