ASP.Net:无法使用 cshtml 中的复选框制作工作下拉列表

GCD*_*CDX 0 asp.net razor

该项目是在 ASP.NET 中完成的,并编写为 web api 和 mvc 项目。

目前我正在尝试使用此自定义 html 和 css 代码来制作带有复选框的下拉列表。但是,我不知道如何映射/保存/绑定到模型中?有没有帮助的解决方案?

下面是使用的代码。它创建了一个带有复选框的下拉列表,但我似乎无法将其发布到模型中。

<form>
    <div class="multiselect">
        <div class="selectBox" onclick="showCheckboxes()">
            <select>
                <option>Select an option</option>
            </select>
            <div class="overSelect"></div>
        </div>
        <div id="checkboxes">
            <label for="one">
                <input type="checkbox" id="one" />First checkbox
            </label>
            <label for="two">
                <input type="checkbox" id="two" />Second checkbox
            </label>
            <label for="three">
                <input type="checkbox" id="three" />Third checkbox
            </label>
        </div>
    </div>
</form>

<style type="text/css">
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}
</style>

        var expanded = false;

        function showCheckboxes() {
            var checkboxes = document.getElementById("checkboxes");
            if (!expanded) {
                checkboxes.style.display = "block";
                expanded = true;
            } else {
                checkboxes.style.display = "none";
                expanded = false;
            }
        }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我如何将它绑定到模型以添加值?

Cha*_*mar 5

请看看

如何在 Select Option 中使用 Checkbox

https://www.c-sharpcorner.com/article/dropdownlist-multi-select-with-checkboxlist-in-mvc-using-jquery/

除此之外请注意,您不能直接从多选下拉绑定模型属性。您需要利用集合属性。即IEnumerable(列表)。

  • 我为您创建了一个演示,用于从 MultiSelect DropDown 绑定模型。只需从下面的链接下载 zip 即可。如果您还有任何疑问,请在这里告诉我。https://drive.google.com/file/d/1nC-07WzBu_Y6m21PKuw6bVdEMZCqvnY1/view?usp=sharing 如果有效,请接受答案。 (2认同)