0

View:

<select multiple="" id="e2" tabindex="-1" class="select2-hidden-accessible" name="e2" aria-hidden="true">
    <option value="PDS.RICE">PDS.RICE</option>
    <option value="PDS.WHEAT">PDS.WHEAT</option>
    <option value="PDS.DAL CHANNA">PDS.DAL CHANNA</option>
    <option value="PDS.DAL KALA CHANNA">PDS.DAL KALA CHANNA</option>
</select>

Controller:

public ActionResult Getitems()
{
    var items = (from m in db.ItemMasters select m).ToList();
    ViewBag.items = items;
    return View();
}

Model:

public class ItemMaster
{
    [Key]
    public int ItemId { get; set; }
    public string Item_Name { get; set; }
}

I am trying to fetch the data from database in the controller, but unable to bind the ViewBag data to the select2-hidden-accessible in view.

3
  • 1
    What have you tried? And why are you not using @Html.DropDownListFor() and strongly bind to a model? Commented Dec 16, 2015 at 6:54
  • i have tried @Html.DropDownListFor(), but did not get the solution. Commented Dec 16, 2015 at 7:26
  • 1
    Interesting - works for me and millions of others. Commented Dec 16, 2015 at 7:27

1 Answer 1

1

You can bind dropdownlist by using ViewBag, below is the code :

Controller :

public ActionResult Getitems()
        {
            var itemList = new List<SelectListItem>
            {
                new SelectListItem
                {
                    Text = "RICE",
                Value = "RICE"
                },
                 new SelectListItem
                {
                    Text = "WHEAT",
                Value = "WHEAT"
                }
            };



            ViewBag.ItemList = new SelectList(itemList, "Value", "Text");
            return View();
        } 

View :

Add below scripts in your head tag.

<head>
    <script src="~/Scripts/jquery-1.8.2.js"></script>
    <script src="~/Scripts/select2.js"></script>
    <link href="~/Content/select2.css" rel="stylesheet" />
</head>

 @Html.DropDownList("e2", (SelectList) ViewBag.ItemList, new {@class = "js-example-basic-multiple",multiple="multiple" })

Initialize the select2 plugin.

<script>
        $(".js-example-basic-multiple").select2();
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

It's a tried and tested code. What exactly you did with the above code?
I have edited my answer please check now and mark as answer if your issue is resolved.
hi i tried your code but not working for me any work around?
What is the issue that you are facing?
View: @Html.DropDownList("e2", (SelectList)ViewBag.ItemList, new { @class = "select2-hidden-accessible" }) <script src="~/assets/js/jquery-2.0.3.min.js"></script> <script src="~/assets/js/select2/select2.js"></script> <script> $("#e2").select2({ placeholder: "Select Commodity", allowClear: true }); </script>
|

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.