How to submit form on dropdown change using jQuery
Step 1
- HTML code
@using (Html.BeginForm("Index", "Dashboard"))
{
@Html.DropDownListFor(model => model.vendor_id, new SelectList((System.Collections.IEnumerable)TempData["vendorslist"], "vendor_id", "name"), new { @class = "form-control", @required = "required" })
}
Step 2
- Csharp code to bind ddl
private void BindDropdowns()
{
List<ourvendor> vlist = new List<ourvendor>();
using (DAL rep = new DAL())
{
vlist = rep.Vendors();
}
vlist.Insert(0, new vendor { name = "--All--" }); // Set Default value
TempData["vendorslist"] = vlist;
}
public ActionResult Index()
{
BindDropdowns();
}
[HttpPost]
public ActionResult Index(string fc,int vendor_id)
{
}
Step 3 - Java script
<script>
$("#vendor_id").on("change", function (event) {
var form = $(event.target).parents('form');
form.submit();
});
</script>
Share This with your friend by choosing any social account