【问题标题】:how to filter records on change of dropdown in mvc Razor如何过滤 mvc Razor 中下拉列表更改的记录
【发布时间】:2018-05-08 18:44:15
【问题描述】:

我有一个要求,比如我有一个在页面加载时绑定的供应商列表,并且我还有下拉列表,用于过滤供应商在下拉列表更改时的记录。

我的看法:

<div class="half-left">
            @Html.DropDownListFor(x => x.subCatObj.SubCategory_Id, Model.GetAllSubCategory, "--Select--", new { @id = "subCategory", @class = "contactField" })
        </div>


        @foreach (var vendor in Model.GetAllVendors)
        {
              @vendor.VendorCompanyName  
        }

我的问题是如何过滤下拉更改的记录..请帮助

【问题讨论】:

  • 使用 Jquery 和 ajax 在过滤下拉列表的更改事件中填充供应商下拉列表。或者,将所有供应商存储在客户端的 javascript 数组中,并编写一个 javascript 函数,该函数执行您的过滤逻辑以填充供应商数组中的供应商下拉列表,并使用您的过滤逻辑进行过滤
  • 我想用剃须刀使用它,有没有使用剃须刀的解决方案
  • 按照您的设置,您可以在 javascript 函数中执行过滤逻辑,并为每个不应出现在将显示属性设置为无的下拉列表中的选项添加一个类,这样你可以保留你的剃刀代码,但仍然可以完成下拉列表的过滤。
  • 谢谢瑞恩..你能分享一个例子,这样我就可以很容易地得到它..我是编程新手
  • 你知道如何使用 CSS 吗?您在网页中使用 JQuery 吗?

标签: jquery ajax asp.net-mvc partial-views


【解决方案1】:

这是一个可能的解决方案:

在您的 CSS 中:

//CSS class to use on your option elements to make their display property none
.hiddenOption{
   display:none;
}

如果使用要导入视图的外部 javascript 文件,或视图中的脚本标记,使用 JQuery,请使用 document.ready 将更改事件附加到子类别下拉列表:

//Use document.ready in JQuery to add change handler to drop down
$(document).ready(function(){
    $('#subCategory').on('change', function(){
         //Call your filtering function here
         FilterVenders();
    });
});

//Declare and define a function for doing the filtering in your external
//script or in your script tag of View
function FilterVenders(){
    //As I don't know what kind of logic you use to filter, you need to
    //Work on this portion
    const subCategoryValue = $('#subCategory').val();
    const subCategoryText = $('#subCategory option:selected').text();

    //Loop all the options in your drop down list and apply the class
    //which makes their display as none (hide the options)
    $('#subCategory > option').each(function(){
         //If the option is not to be displayed, add the class
         //This is where you need to step in and provide the filtering logic
         if(condition){
            //Check if option is already hidden, if not add the class to hide it
             if(!$(this).hasClass('hiddenOption')){
                 $(this).addClass('hiddenOption');
             }
          }else{
             //Check if option is hidden, if so, remove class to unhide it
             if($(this).hasClass('hiddenOption')){
                 $(this).removeClass('hiddenOption');
             } 
          }
     });
}

【讨论】:

  • 谢谢瑞恩..知道了
  • @sumit.spider 好的。如果您对我提供的答案感到满意,请将其标记为已接受的答案。
猜你喜欢
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多