简体   繁体   English

DropDown(组合框)与ASP.NET MVC中的复选框

[英]DropDown(combobox) with checkboxes in ASP.NET MVC

Hi, 嗨,

I am building a ASP.NET MVC 2 application and have so far used regular dropdowns : 我正在构建一个ASP.NET MVC 2应用程序,到目前为止使用常规下拉列表:

<%: Html.DropDownListFor(model => model.LS.L1, Model.LS.Location1List, "-- choose place --", new { @class = "dd1" })%>

Now I need to change this to a dropdown with checkbox list instead. 现在我需要将其更改为带有复选框列表的下拉列表。 This means two things : 这意味着两件事:

  1. The model most be changes so L1 can hold multiple values(instead of just a int), maby also the Location1List most be changed from SelectList to some kind of list? 模型大多数都是变化的,所以L1可以保存多个值(而不仅仅是一个int),maby也可以将Location1List从SelectList更改为某种列表?
  2. The DropDownListFor(combobox) most be changed to a "DropDownCheckListFor. DropDownListFor(组合框)大多数都改为“DropDownCheckListFor”。

Its important that this is as basic as possible so we can maintain browser compability(even on smartphones). 重要的是,这是尽可能基本的,以便我们可以保持浏览器的兼容性(即使在智能手机上)。

As you can see its also important that the selected values is filled into a model object(by the default binder) on submit. 您可以看到,在提交时将所选值填充到模型对象(通过默认活页夹)中也很重要。

I have looked at this dropdown : http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ 我查看了这个下拉列表: http//www.erichynds.com/jquery/jquery-ui-multiselect-widget/

Its looks grate but Im not sure how to translate this to ASP.NET MVC so the selected values is filled in to a model object on submit(as the DropDownListFor does). 它看起来很不错,但我不知道如何将其转换为ASP.NET MVC,因此所选值将填充到提交的模型对象中(如DropDownListFor所做)。 Also this jquery solution might be unnecary complex. 此jquery解决方案也许不必要复杂。

Pleas advice 请求建议

Remember MVC helpers just render HTML markup. 记住MVC助手只是呈现HTML标记。 So according to the instructions on that page you linked, you just need generate 因此,根据您链接的页面上的说明,您只需要生成

<select multiple="multiple">

In order to do that you need to use Html.ListBoxFor instead of Html.DropDownListFor . 为此,您需要使用Html.ListBoxFor而不是Html.DropDownListFor Then when you've included all of the correct javascript files, you can simply add a javascript block. 然后,当您包含所有正确的javascript文件时,您只需添加一个javascript块。 That looks like this: 看起来像这样:

$(function() {
   $('.dd1').multiselect();
});

This tutorial shows complete step by step guide for creating multiselect dropdown with checkboxes in mvc Multiselect dropdown with checkboxes in MVC 本教程显示了创建多选下拉列表的完整分步指南,其中包含mvc Multiselect下拉列表中的复选框以及MVC中的复选框

You Need Select Tag 你需要选择标签

<select id="CustomerId" name="CopyFromCustomerId"></select>

or you can use Html.ListBoxFor 或者你可以使用Html.ListBoxFor

You need following script 您需要以下脚本

$('#CustomerId').multiselect({
            includeSelectAllOption: true
        });

And below css and script files 并在css和脚本文件下面

<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>     
<script type="text/javascript" src="http://davidstutz.github.io/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="http://davidstutz.github.io/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css"/>

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

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