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?
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.
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.
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).
You can use the onchange event to submit the current form:
<%=Html.DropDownList("values", ViewData["ListData"],
new { onchange="this.form.submit();" })%>
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();
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With