I'm trying to pass a string from a "text" field to an MVC action. The form variable is called query. The Javascript code is:
<input type="text" class="form-control input-lg" placeholder="search..."
name="SearchBar" onkeypress="searchfunction()" />
<script>
function searchfunction()
{
if (event.keyCode == 13) {
var url = '"' + document.getElementById("#SearchBar").value + '"';
window.location = "@Url.Action("~/Home/Search")?query=" + url;
}
}
</script>
The URL generated from this should be domain/Home/Search?query=[contents of textfield] but for some reason it's generating a URL like domain/search.html?SearchBar=[contents of SearchBar] and I get 0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference. I am sure those two are related, but .value is what EVERY example I can find recommends using.
Note: I tried using the getElementById both with # and without, same for the name property of the input.
Should I just be passing the string in the onkeypress event? Am I missing something simple? Is this a bug in MVC 3?