Reputation: 506
I have a site that searches through a db and pages the results. I want to make it so whenever there is a new search the page index defaults to 1. To do this I want to pass a flag when the submit button is pressed to tell the controller to modify the page index. I thought you might be able to pass the value with the onclick attribute but haven't had any success.
Here is my code for the search text box and submit button (without my failed attempt at using the onclick attribute).
Find: @Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type = "submit" value = "Search"/ >
Upvotes: 1
Views: 1740
Reputation: 22485
this answer is supplied to address your original question in order to not require major refactoring (see final comment for my suggestion). so, use a hidden input as such:
Find: @Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type="hidden" name="lastSearchValue" ViewBag.LastSearch as string />
<input type = "submit" value = "Search"/ >
then, when you POST the form, check to see if the value of lastSearchValue
and SearchString
are the same, if so do what you need to do. if i understand your reasoning, then the final step would be to set ViewBag.LastSearch
to the value of the last set ViewBag.CurrentSearch
.
A better solution would be to use a SearchViewModel
to encapsulate all of this logic in a self contained way. This gives the benefit of not having disparate logic spread across different concerns.
Upvotes: 1
Reputation: 5799
You can pass the flag as a hidden input on the form:
@Html.Hidden("FlagValue", Your_Flag_Value);
Find: @Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type = "submit" value = "Search"/ >
Upvotes: 0
Reputation: 17584
You could use a hidden input:
Find: @Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type="hidden" name="resetPageIndex" value="true" />
<input type = "submit" value = "Search"/ >
The input value will get sent with the form submission. You'll have to update the POST parameter accepted by your controller accordingly.
Upvotes: 1