Chosen and Jquery and MVC

In the MVC forums I have seen a reference to Chosen. From the description here:
“Chosen is a JavaScript plugin that makes long,unwieldy select boxes much more user-friendly.”
It look beautifool – and I see the opportunity to improve my toolbox. I said: “I will do in 10 minutes”. Well,it did take longer – but not so long.

I will make an example my tutorial mvc and ajax – since it does not require any database.

So I download the source from https://github.com/harvesthq/chosen/,put the .js in Scripts folder,the .css in Contents folder and registered in JqueryMVCRazor\JqueryMVCRazor_Web\Views\Shared\_Layout.cshtml

<link href="@Url.Content("~/Content/chosen.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/chosen.jquery.min.js")" type="text/javascript"></script>

Here is the code : ( from JqueryMVCRazor\JqueryMVCRazor_Web\Views\Shared\EditorTemplates\EditEmployeeViewModel.cshtml ):

 @{
     var idcombo = "#"+ ViewData.TemplateInfo.GetFullHtmlFieldId("cmbemp" + Model.Employee.IdEmployee);
     }<script>
     $(document).ready(function () {

         $("@idcombo").chosen({ no_results_text: "No results matched"});

     });

 </script>

And I said,this will be all.

Not so: see the picture – the names does not show up:

image

First I was thinking that it does not select the item. After several attempts,I realize the “…” ( three dots) – that means the width was not enough.
Next move,since I was in the javascript mood,was to add some javascript to adjust the width to fit:

So the code becomes:

$("@idcombo").chosen({ no_results_text: "No results matched" });
$("@idcombo").width("350");

No difference. So I tried the opposite way:

$("@idcombo").width("350");
$("@idcombo").chosen({ no_results_text: "No results matched" });

Now it matches:
image

And,thinking about a bit,it is better right in the Dropdown declaration :

@Html.DropDownList("cmbemp" + Model.Employee.IdEmployee,new SelectList(Model.DepartmentList,"IDDepartment","NameDepartment",Model.Employee.Department.IdDepartment),new{ style="width:350px;"})

Summary:

Integration of Chosen with MVC dropdown is rather simple. Add the .js,the .css,register in the page,add $(“@idcombo”).chosen() declaration and do not forget the width of the dropdown ( select )

Source code here
Later edit: Please add also

to the header


Posted

in

by

Tags:

Comments

9 responses to “Chosen and Jquery and MVC”

  1. goran Avatar
    goran

    Hi, can you post a simple version of using choosen for dropdown, example: one action that send list of items from controller and dropdown in view (referencing jscript code and using html helper), i am curios of sending selected value from choosen dropdown.

    1. Andrei Ignat Avatar
      Andrei Ignat

      Sure.
      Look at
      http://pmkb.codeplex.com/
      You have “edit link” – just edit a link and you will see a list of tags.
      More simply :
      in the LinkAddOrEdit.cs file I have
      public List IDSelectedTags { get; set; }
      This is automatically filled by MVC

  2. goran Avatar
    goran

    LOL, i think you didn’t understand my question, and i don’t see any “edit link” on http://pmkb.codeplex.com/
    I just wanted to see basic usage of choosen dropdown, but nevermind i created it myself and testing right now, and when it comes to get selected value it’s pretty easy to collect it with jquery .val() or getselectedvalue().

    1. Andrei Ignat Avatar
      Andrei Ignat

      If you want on client side, it’s the same as any dropdown (* chosen actually hides the dropdown)
      For server side, you will add an int or an List ( for multiple)

  3. goran Avatar
    goran

    Did you try to create choosen dropdown with groups like the “Single Select with Groups” on http://harvesthq.github.com/chosen/
    that would be very nice for users 🙂

    1. Andrei Ignat Avatar
      Andrei Ignat

      It requires some syntax to think about…

  4. D Avatar
    D

    Thanks, this is exactly what I’ve been looking for. I’m hoping to use partial views to implement both Chosen and JEditable.. we’ll see. I really want to show the table as plain text until the user double clicks a cell, and then have it be editable, only using Chosen as the plugin. THAT my friend, would be a terrific tutorial 🙂 (not to say this isn’t, this tutorial is great as well)

  5. Gokul Avatar
    Gokul

    Hello There,
    Thank You for this article.

    http://harvesthq.github.com/chosen/

    Please goto the above url and see the multi select option My requirement is to use the same selector but I want the the auto selection to happen only when I enter a special Character like #

    For example I want to type in the input box like

    I lived in countries like #India #USA with my @friend-1 and friend-2

    So the below drop-down should show a list of countries when I press # and when I press @ it should display a list of my contacts

    Could you please help ?

    1. Andrei Ignat Avatar
      Andrei Ignat

      You modify the list when typing. It’s easy if you know javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *