简体   繁体   English

在MVC中级联所需的下拉菜单

[英]cascading required dropdowns in MVC

Any idea on how to implement 2 Required cascading dropdowns in ASP.Net MVC? 关于如何在ASP.Net MVC中实现2个必需的级联下拉菜单的想法? I am stressin on the attribute [Required] because I already created the cascading dropdowns. 我强调[必需]属性,因为我已经创建了级联下拉列表。 My issue is, the child dropdown should show depending on certain values of the parent dropdown. 我的问题是,子下拉列表应根据父下拉列表的某些值显示。 But since both are decorated with the Required attribute, in case the child dropdown is not shown, the form does not submit. 但是,由于两者都使用Required属性进行了修饰,因此如果未显示子项下拉列表,则不会提交表单。 which is normal since both dropdowns are part of the form. 这是正常的,因为两个下拉菜单都是表单的一部分。 How can I implement both dropdowns to be required but only if the child is visible on the form? 如何实现两个必填项,但前提是该子项在表单上可见? is JavaScript the only way to do it? JavaScript是唯一的方法吗? Thanks 谢谢

Assuming you are using jQuery Validation Plugin, required only if #other is selected: 假设您使用的是jQuery Validation插件,仅在选择#other时才需要:

$("#myform").validate({
  rules: {
    dropdown1: {
      required: true
    },
    dropdown2: {
      required: function(element) {
        return $("#dropdown1").val() != "";
      }
    }
  }
});

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

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