简体   繁体   English

使用按钮将ASP.NET MVC设置下拉回默认值

[英]ASP.NET MVC Setting dropdowns back to default value with a button

Using ASP.NET MVC i created a few dropdown menu's that filter a list depending on which value you select from these dropdown menu's. 使用ASP.NET MVC,我创建了一些下拉菜单,这些菜单根据您从这些下拉菜单中选择的值来过滤列表。 I'm trying to create a buttton that sets all the dropdowns back to the standard value, essentialy "Resetting" the dropdown menu's. 我正在尝试创建一个将所有下拉菜单都设置回标准值的按钮,本质上是“重置”下拉菜单。 After this refresh i would like to submit the form automatically. 刷新后,我想自动提交表单。

@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
}

With some help from people in the comment section, and some asking around at my work i found a solution that works just fine for me. 在评论部分中一些人的帮助下,以及在我的工作中四处询问时,我找到了一个对我来说很好的解决方案。 I decided to delete all the selected attributes from the dropdown options and post the form. 我决定从下拉选项中删除所有选定的属性,然后发布表单。

JQuery: 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM