9

I am working on MVC4 project where i have a multi select dropdown

@Html.DropDownList("year_selected", (SelectList)(ViewData["YearSelected"]), new { tabindex = "14", multiple = "multiple", style = "width:150px;height:200px;" })

Its populated with list of years i have mentioned in controller

int minYear =Int32.Parse(Helper.MinYear);
int maxYear = Int32.Parse(Helper.MaxYear);
var yearSelectedList = new List<SelectListItem>();
for (int count = minYear; count <= maxYear; count++)
{
    yearSelectedList.Add(new SelectListItem()
    {
        Text = count.ToString(),
        Value = count.ToString()
        });
    }
    var yearselectlist = new SelectList(yearSelectedList, "Value", "Text");
    ViewData["YearSelected"] = yearselectlist;

and on dropdown click i am calling jquery to select that particular value and also when page is loaded i checking values saved in database by making them select by default

Here is jquery code to selected the values which are saved in database

if (str_year_selected.val() != "") {
    var yeararray = str_year_selected.val().split(",");
    for (var i in yeararray) {
         var val = yeararray[i];
         year_selected.find('option:[value=' + val + ']').attr('selected', 1);
    }
}

and here is code i am using to select the value when user clicks or press ctrl key on dropdown values

year_selected.change(function () {
     var selectedyears = "";
     $("#year_selected :selected").each(function (i) {
        if (i != 0) {
            selectedyears += ",";
        }
         selectedyears += $(this).text();
     });
     str_year_selected.val(selectedyears);
});

Everything is working perfect . but now problem is user wants checkbox inside of dropdown so that can check the option.

How do i do this ?

1

3 Answers 3

8

Rather than using dropdown you can use checkbox in div with css. It will fell like it is a dropdown list with checkbox.

Here is good link of jquery

http://www.erichynds.com/blog/jquery-ui-multiselect-widget

Sign up to request clarification or add additional context in comments.

Comments

6

user wants checkbox inside of dropdown so that can check the option.

There is a fantastic JQuery Plugin called Dropdown Check List that transforms a regular select HTML element into a dropdown checkbox list.

enter image description here

Download

Comments

1

This tutorial shows complete step by step guide for creating multiselect dropdown with checkboxes in mvc Multiselect dropdown with checkboxes in MVC

You Need Select Tag

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

or you can use Html.ListBoxFor

You need following script

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

And below css and script files

<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"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.