In my MVC3 View I have a textbox linked to a Model property, like so:
<tr>
<td>Name contains:</td>
<td>@Html.TextBoxFor(model => model.NameContains, new { @style = "width:99%" })</td>
<td>@Html.ImageButton("Search", "nameContainsSearchButton")</td>
</tr>
My Image button is used in a piece of script to call the appropriate method on the controller that returns a partial view, like so:
<script type="text/javascript">
$(function () {
$('#nameContainsSearchButton').click(function () {
var data = $("#NameContains").val();
// send an AJAX request to the controller action
$('#searchResults').load("/QuickSearch/Search", data);
return false;
});
});
As you can see, I am trying to pass the value of the TextBox 'NameContains' as a parameter to my controller method. However, the parameter received by the method is always null.
Anybody have any idea what I am doing wrong?
Thanks in advance,
Lucien.
EDIT: I got the above working by changing the call to:
$('#searchResults').load("/QuickSearch/Search", {nameContains: data});
where nameContains is the name of the parameter in the action method.
I have a follow-up question, though: how can I pass the entire Model as a parameter to an action method? There's another page where the user can enter many search criteria and I want to just pass the entire search criteria model object to the controller....