0

I have a searchform in my mvc application where i choose in a dropdownlist what department i want to search in, and a textinput on what i want to search on.

when i choose a department in my dropdownlist i want to forms controller to change to the selected value.

Lets say i have these items i my dropdownlist: "Customers", "Products", "Invoices"

when i choose "Customers" in my dropdownlist i want my Html.BeginFrom to look like this:

<% using (Html.BeginForm("Customers", Request.QueryString["Search"], "Search", FormMethod.Get))
       { %>

and when i select "Products" i want "Customers" to change to "Products".

Raskolnikoov
  • 565
  • 11
  • 27

2 Answers2

0

This is generally not the way you do things.

If you need two different things to happen depending on the posted value of a dropdown you should handle the logic dispatching inside of the Controller.

public ActionResult DoSomething(string actionType )
{
     if( actionType == "Products" )
          DoSomething();

     if( actionType == "Customers" )
          DoSomethingDifferent();
}
John Farrell
  • 24,673
  • 10
  • 77
  • 110
0

I agree this isn't the best way to approach things, however if you still need to do it this way then a simple jquery statement is what you'll need.

Basically handle the onSubmit action of the form (or the onChange of the dropdown list --both will work) and change the form's action based on the value of the drop down list

Something like this should work:

<% using (Html.BeginForm()) { %>
  <%=Html.DropDownListFor( x => x.DepartmentList ) %>
  <input type='submit' value='submit'/>
<% } %>

<script type="text/javascript">
 $(function(){
   $('#DepartmentList').change(function(){
      $('form').attr('action', $('#DepartmentList option:selected').val() );
   });
 });
</script>

Check out the answer of this question for more details

Community
  • 1
  • 1
TheRightChoyce
  • 3,054
  • 1
  • 22
  • 19