C# MVC下拉列表中的If-else条件

C# MVC下拉列表中的If-else条件,c#,javascript,jquery,asp.net-mvc,razor,C#,Javascript,Jquery,Asp.net Mvc,Razor,我的web应用程序上有两个下拉列表。两个下拉列表的填充数据来自数据库。它们包含相同的数据 我想要的是,如果在第一个下拉列表中选择了其中一个列表,那么在第二个下拉列表中该列表将不再可用 我有以下语法: @Html.DropDownListFor(model => model.Questions1, (SelectList)ViewData["Questions"], "Select>>", new { id = "Questions1", Name = "Questions1"}

我的web应用程序上有两个下拉列表。两个下拉列表的填充数据来自数据库。它们包含相同的数据

我想要的是,如果在第一个下拉列表中选择了其中一个列表,那么在第二个下拉列表中该列表将不再可用

我有以下语法:

@Html.DropDownListFor(model => model.Questions1, (SelectList)ViewData["Questions"], "Select>>", new { id = "Questions1", Name = "Questions1"})

@Html.DropDownListFor(model => model.Questions2, (SelectList)ViewData["Questions"], "Select>>", new { id = "Questions2", Name = "Questions2"})
问题来自从数据库检索的模型


提前谢谢

不确定是否有更巧妙的方法,但以下是我的想法

克隆选项元素 添加更改侦听器 在2中重新创建选项 从2中删除1中选择的一个
您可以使用Jquery函数

如果你有两个下拉列表

<select id="dd1" style="width:200px;">
              <option value="feedback" name="aft_qst">After Quest</option>
              <option value="feedback" name="aft_exm">After Exam</option>
              </select>



<select id="dd2" style="width:200px;">
              <option value="feedback" name="aft_qst">After Quest</option>
              <option value="feedback" name="aft_exm">After Exam</option>
              </select>

请看这里:

为了实现这一点,您需要将选项池存储在javascript对象中。然后,在每个下拉列表的“onchange”事件中,重新构建另一个下拉列表中的选项,不包括所选的选项。下面是一个使用jQuery的示例:

// Build a javascript array with all of the select names/values
var options = new Array();
$('#Questions1 option').each(function() {
    $this = $(this);
    options.push({ Name: $this.text(), Value: $this.val() });
});

// Create a function for re-building a select minus the chosen option
var rebuildSelect = function($selOption, $select) {
    $previouslySelected = $select.find(':selected');
    $select.empty();
    for (var i = 0; i < options.length; i++) {
        var opt = options[i];
        if (opt.Value != $selOption.val()) {
            if ($previouslySelected.val() == opt.Value) {
                $select.append('<option value="' + opt.Value + '" selected="selected">' + opt.Name + '</option>');
            }
            else {
                $select.append('<option value="' + opt.Value + '">' + opt.Name + '</option>');
            }
        }
    }
}

// Wire up the event handlers
var $Questions1 = $('#Questions1');
var $Questions2 = $('#Questions2');

$Questions1.change(function() {
    rebuildSelect($(this), $Questions2);
});

$Questions2.change(function() {
    rebuildSelect($(this), $Questions1);
});

// Go ahead and run the function on each box to remove the default entries from the other box
rebuildSelect($Questions1.find(':selected'), $Questions2);
rebuildSelect($Questions2.find(':selected'), $Questions1);

这不是一个直接的答案,而是一个选择

为什么不在下拉列表中保留这两个选项,让用户选择相同的项目,当他们选择时,显示一条消息,如请选择两个不同的项目等?你的代码也应该更简单


如果我是一个用户,无论出于什么原因,我想选择两次相同的东西,我想我宁愿系统让我这样做,然后告诉我我做错了,而不是尝试选择两次相同的东西,然后在第二个下拉列表中发现我想要的东西丢失了,引起我一时恐慌。

我想你应该做一件事。。如果其中一个被更改,它将从另一个下拉列表中删除该项。。问题是当你再次改变它时。。删除的项目将不再显示。请尝试创建一个事件,当您更改下拉列表中的选择时。。它将自动检查当前所选项目是否与您正在选择的项目相同。。因此,您可以提示或不接受用户选择的内容。如果这种情况立即发生,并且没有回帖?建议这样做,在这种情况下,这样做可能会更简单。您可以响应JavaScript中select元素的更改,并从另一个select中删除任何匹配选项。您只需要将这些值存储在另一个结构中,以便在值再次更改时重新添加它们。是的,应该在没有回传的情况下完成这正是我想要做的。但是,我的下拉列表中没有硬编码的html,因此我可能会对该函数进行一些调整。谢谢你!谢谢你。这对我有用。但是,我有两个下拉列表的默认值选择>>请参阅问题更新。因此,当加载页面时,第二个下拉列表没有选择>>选项。谢谢你的建议。然而,这是给我的业务要求。@杰拉尔德:你的建议很好。我知道做比做我想做的更容易。只是有些事情我们无法控制:
$("#dd1").change(function(){
        $("#dd2").prop("disabled", true);
});
// Build a javascript array with all of the select names/values
var options = new Array();
$('#Questions1 option').each(function() {
    $this = $(this);
    options.push({ Name: $this.text(), Value: $this.val() });
});

// Create a function for re-building a select minus the chosen option
var rebuildSelect = function($selOption, $select) {
    $previouslySelected = $select.find(':selected');
    $select.empty();
    for (var i = 0; i < options.length; i++) {
        var opt = options[i];
        if (opt.Value != $selOption.val()) {
            if ($previouslySelected.val() == opt.Value) {
                $select.append('<option value="' + opt.Value + '" selected="selected">' + opt.Name + '</option>');
            }
            else {
                $select.append('<option value="' + opt.Value + '">' + opt.Name + '</option>');
            }
        }
    }
}

// Wire up the event handlers
var $Questions1 = $('#Questions1');
var $Questions2 = $('#Questions2');

$Questions1.change(function() {
    rebuildSelect($(this), $Questions2);
});

$Questions2.change(function() {
    rebuildSelect($(this), $Questions1);
});

// Go ahead and run the function on each box to remove the default entries from the other box
rebuildSelect($Questions1.find(':selected'), $Questions2);
rebuildSelect($Questions2.find(':selected'), $Questions1);