Wednesday, April 10, 2013

How to get/set Selected Values of Select2 Multi Selection List in MVC?

Hi 
Here  you  can  get  selected element of  Multi selection list  using  Select2 Jqyery.

Jquery Code

var AuthorList=  $("select#AuthorList").select2("val");
-----------------------------------------------------------------------

2. How to set MultiSelection List?

    First  Get  Selected  value  from  your  database  and  add  into   Hidden Field
like.



Then
Var authlist="";
  var a_options = [];

        $.each($("#AuthorListoption"), function () {
            if (authlist.toLowerCase().indexOf($(this).attr("value")) >= 0) {
                a_options.push($(this).attr("value"));
            }

        });

        $("select#AuthorList").select2("val", a_authlist);

HAPPY CODING



How To Bind MultiSelection List with Select2 Jquery in MVC

Action
------------------
Public ActionResult Author()
{
     SelectList AuthorList= null;
            IEnumerable Author = AuthorList.AsEnumerable().Select(c => new SelectListItem()

           {
               Text = objauth.ReplaceTagValueToTerm(c.TagValue, c.Term),
               Value = c.Id.ToString(),
               Selected = true,
           });
           Newterms = new SelectList(Author, "Value", "Text");
            ViewBag.AuthorList= Newterms;
}
}

-------------------------------------------------------------------------------------
View Page
     @Html.DropDownList("infoTypes", (SelectList)ViewData["AuthorList"], new { @class = "NewCssdropdown", multiple = "" })
---------------------------------------------------------------------
Jquery

  $("#AuthorList").select2(
        {
            maximumSelectionSize: 4 //  Here You can define maximum selection


        });


HAPPY CODING

How to Bind Select2 Jquery in MVC

Action
------------------
Public ActionResult Author()
{
     SelectList Newterms = null;
            IEnumerable Author = AuthorList.AsEnumerable().Select(c => new SelectListItem()

           {
               Text = objauth.ReplaceTagValueToTerm(c.TagValue, c.Term),
               Value = c.Id.ToString(),
               Selected = true,
           });
           Newterms = new SelectList(Author, "Value", "Text");
            ViewBag.NewTerms = Newterms;

}
}

-------------------------------------------------------------------------------------
View Page
    @Html.DropDownList("NewTerms", (SelectList)ViewData["NewTerms"], new { @class = "NewCssdropdown" })


--------------------------------------------------------------------------------------
Jquery


---------------------------------------------------------------------------------------------





HAPPY CODING

How to get selected Value from Select2 Jquery.

Hi To know how to bind Dropdown using Select2. Please reffer  my  old  Post

How To Bind Select2

Now i  will  give you  one example to Get Selected Value.


Jquery
--------
  var AuthorList= $("#AuthorList").val();

HAPPY CODING