Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a dropdownlist to post a form in MVC

Tags:

asp.net-mvc

I've got a simple MVC view with a dropdown and a Submit button that posts the form and uses the value of the dropdown to change the view ("values" is populated in the controller):

<% Using Html.BeginForm()%>
<%=Html.DropDownList("values", "No value")%>
<input type="submit" value="Submit" />
<%--rest of page here--%>
<% End Using%>

How can I set this up to change once the dropdown is changed, i.e. eliminate the Submit button?

like image 678
gfrizzle Avatar asked Mar 17 '09 17:03

gfrizzle


People also ask

How get dropdown selected value from view to controller in MVC?

If you want to pass the selected value of your dropdown list into your controller, just add a string parameter with the name drpFields (same as the first parameter of your DropDownList method) into your temp action method. Hope it will help you to fix your problem.

How can use static dropdown in MVC?

Binding MVC DropDownList with Static Values Just add an Html helper for DropDownList and provide a static list of SelectListItem. The values added as SelectListItem will be added and displayed in the DropDownList. In this way, you do not need to add anything to Controller Action.

What is right way to define routes MapRoute () in MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).


2 Answers

You can use the onchange event to submit the current form:

<%=Html.DropDownList("values", ViewData["ListData"],
                      new { onchange="this.form.submit();" })%>
like image 70
Christian C. Salvadó Avatar answered Sep 28 '22 07:09

Christian C. Salvadó


I would use jquery to make the change

first add the class to your submit button and dropdown list as so:

     <%=Html.DropDownList("values", "No value",new {_class="dropdown"})%>

      <input type="submit" value="Submit" class="submit"/>

then use jquery like this:

  $(document).ready(function() {

   $(".dropdown").change({

   $(".submit").hide();
    })
  });
like image 39
TStamper Avatar answered Sep 28 '22 07:09

TStamper