使用按钮将ASP.NET MVC设置下拉列表恢复为默认值

时间:2017-12-12 13:55:31

标签: c# asp.net-mvc

使用ASP.NET MVC我创建了一些下拉菜单,根据您从这些下拉菜单中选择的值来过滤列表。我正在尝试创建一个将所有下拉菜单设置回标准值的按钮,实际上是“重置”下拉菜单。刷新后我想自动提交表格。

@using (Html.BeginForm("Info", "Products", FormMethod.Get, new { @class = "form-group" }))
{
@Html.HiddenFor(model => model.productIdCode)

<div class="col-md-3">
    <div class="form-group">
        @Html.DropDownListFor(x => x.laborIdDropdown, ViewBag.LaborId as SelectList, "Lab Code", new { @class = "form-control", @onchange = @"form.submit();" })
    </div>
</div>
<div class="col-md-4">
    <div class="form-group">
        @Html.DropDownListFor(x => x.brandIdDropdown, ViewBag.BrandId as SelectList, "Brand", new { @class = "form-control", @onchange = @"form.submit();" })
    </div>
</div>
<div class="col-md-3">
    <div class="form-group">
        @Html.DropDownListFor(x => x.platformIdDropdown, ViewBag.PlatformId as SelectList, "Platform", new { @class = "form-control", @onchange = @"form.submit();" })
    </div>
</div>
<div class="col-md-2">
    <div class="form-group">
        @Html.DropDownListFor(x => x.cultureIdDropdown, ViewBag.CultureId as SelectList, "Culture", new { @class = "form-control", @onchange = @"form.submit();" })
    </div>
</div>
//Button that sets all dropdowns to zero and submits form
}

1 个答案:

答案 0 :(得分:0)

在评论部分的人们的帮助下,有些人在我的工作中询问,我找到了一个对我来说很好的解决方案。我决定从下拉选项中删除所有选定的属性并发布表单。

JQuery的:

$('#resetButton').click(function (e) {
    e.preventDefault();

    $('#laborIdDropdown > option:selected').removeAttr("selected");
    $('#brandIdDropdown > option:selected').removeAttr("selected");
    $('#platformIdDropdown > option:selected').removeAttr("selected");
    $('#cultureIdDropdown > option:selected').removeAttr("selected");

    $("#filterForm").submit();
});