views:

125

answers:

3

Hello,

I am trying to create a form in MVC C# that will allow a user to input a Last Name, First Name, Department, Year and click a Search button that will bring back a list of employees based off the inputted search criteria.

My problem is allowing multiple search textbox criteria into one search button.

I am able to hardcode values into an html actionlink like below and it works but unable to grab the values from the textboxes.

<%= Html.ActionLink("Results", "Results", new { lastName = "Smith", 
    firstName = "", dept = "", year = "2008" } )%>

I would really just like to have four textboxes and a search button to bring the list back from the database.

Thanks for the help.

A: 

I think you need to use a form and a submit button, then you'll get the values of all the input fields in the form, you could do this with ajax so it wont refresh the whole page.

If you dont want the form solution, you could use some javascript (jquery is good) to detect the value change of the textboxes and then grab the value of the textboxes and do an ajax call to the controller, sending this values.

Francisco Noriega
Thanks for the quick reply, would you happen to have an example of this?
GB
+1  A: 

If you have it in a form, you can catch a submit of that together with values for each textbox (or any other tag in the form).

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Results(string lastName, string firstName, string dept, int year)

That method overloads Results and is launched only by a POST request.

Trimack
How would I pass the value from the text boxes which is in Index.aspx to the Results method. I pretty much just need the code that belongs in the aspx file. Thanks for the help.
GB
Well, it already wrote Coov in his response. I guess combination of these two will do what you are looking for.
Trimack
+1  A: 

On your Index.aspx page

<% using (Html.BeginForm()) { %>

   <%= Html.TextBox("firstname") %>
   <%= Html.TextBox("lastname") %>

   <input id="submit1" type="submit" value="Submit" />

<% } %>

This will post the "firstname" and "lastname" form field values and you pick them up in your action like in @Trimack's example.

You can carry the posted data forward to your results page with TempData.

TempData["firstName"] = firstname;

In your results.aspx page you'd have:

<%= Html.Hidden("firstname", TempData["firstName"]) %>
Coov