"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Adding a New Category to the DropDownList using jQuery UI

by Rick Anderson

The HTML Select tag is ideal for presenting a list of fixed category data, but often times you need to add a new category. Suppose we want to add the genre “Opera” to the categories in our database? In this section, we will use jQuery UI to add a dialog box we can use to add a new category. The image below shows how the UI will present in the browser.

When a user selects the Add New Genre link, a pop-up dialog box prompts the user for a new genre name (and optionally a description). The image below show the Add Genre pop-up dialog.

When a new genre name is entered and the Save button is pushed, the following happens:

  1. An AJAX call posts the data to the Create method of the Genre Controller, which saves the new genre to the database and returns the new genre information (genre name and ID) as JSON.
  2. JavaScript adds the new genre data to the select list.
  3. JavaScript makes the new genre the selected item.

In the image below, Opera was added to the database and selected in the Genre drop down list.

Open the Views.cshtml file and replace the genre markup with the following the following code:

[!code-cshtmlMain]

   1:  <div class="editor-field ui-widget">
   2:   
   3:       @Html.Partial("_ChooseGenre")
   4:   
   5:  </div>

The _ChooseGenre partial view will contain all the logic to hook up the JavaScript and jQuery used to implement the add new genre feature. Once we have completed the code it will be simple to do the same with the artist UI.

In Solution Explorer, right click the Viewsfolder and select Add, then View. In the View name input, enter _ChooseGenre then select Add. Replace the markup in the Views\_ChooseGenre.cshtml* file with the following:

[!code-cshtmlMain]

   1:  @model MvcMusicStore.Models.Album
   2:   
   3:  <div class="editor-label">
   4:   
   5:      @Html.LabelFor(model => model.GenreId, "Genre" )
   6:   
   7:  </div>
   8:   
   9:  @Html.DropDownList("GenreId", ViewBag.Genres as SelectList, String.Empty)
  10:   
  11:  <a class="button" href="@Url.Content("~/Genre/Create")" 
  12:   
  13:      id="genreAddLink">Add New Genre</a>
  14:   
  15:  @Html.ValidationMessageFor(model => model.GenreId)
  16:   
  17:  <div id="genreDialog" class="hidden">
  18:   
  19:  </div>
  20:   
  21:  <script src="@Url.Content( "~/Scripts/ui/jquery.ui.combobox.js" )"></script>
  22:   
  23:  <script src="@Url.Content("~/Scripts/chooseGenre.js")"></script>

The first line declares that we are passing in an Album as our model, exactly the same model statement found in the Create view. The next few lines are the Label helper markup. The next line is the DropDownList helper call, exactly the same as in the original Create view. The next line adds a link with the name Add New Genre, and styles it like a button. The line containing ValidationMessageFor is copied directly from the Create view. The following lines:

[!code-htmlMain]

   1:  <div id="genreDialog" class="hidden">
   2:   
   3:  </div>

creates a hidden div, with the ID of genreDialog. We will use jQuery to hook up our Add Genre dialog box with the ID genreDialog in this div. The last two script tags contain links to the JavaScript files we will use to implement the add new genre feature. The /Scripts/chooseGenre.js file is provided for you in the project, we will examine it later in the tutorial.

Run the application and click on the Add New Genre button. In the Add Genre dialog box, enter Opera in the Name input box.

Click the Save button. An AJAX call creates the Opera category and then populates the dropdown list with Opera, and sets Opera as the selected genre.

Enter an artist, title and price, then select the Create button. If you enter a price less than $8.99, the new album will appear at the top of the Index view. Verify the new album entry was saved in the database.

Try creating a new genre with only one letter. The following code in the Models.cs file sets the minimum and maximum length of the genre name.

[!code-csharpMain]

   1:  [StringLength(20, MinimumLength = 2)]
   2:  public string Name { get; set; }

Client side validation reports you must enter a string between 2 and 20 characters.

Examining How a New Genre is Added to the Database and the Select List.

Open the Scripts.js file and examine the code.

[!code-javascriptMain]

   1:  $(function () {
   2:   
   3:      $('#genreDialog').dialog({
   4:   
   5:          autoOpen: false,
   6:   
   7:          width: 400,
   8:   
   9:          height: 300,
  10:   
  11:          modal: true,
  12:   
  13:          title: 'Add Genre',
  14:   
  15:          buttons: {
  16:   
  17:              'Save': function () {
  18:   
  19:                  // Omitted 
  20:   
  21:              },
  22:   
  23:              'Cancel': function () {
  24:   
  25:                  $(this).dialog('close');
  26:   
  27:              }
  28:   
  29:          }
  30:   
  31:      });

The second line uses the ID genreDialog to create a dialog box on the div tag in the *Views\_ChooseGenre.cshtml* file. Most of the named parameters are self explanatory. The autoOpen parameter is set to false, selecting the Create Genre button will open the dialogue explicitly (this is described latter on). The dialog has two buttons, Save and Cancel. The Cancel button closes the dialog. The following code shows the Save button function.

[!code-javascriptMain]

   1:  'Save': function () {
   2:   
   3:      var createGenreForm = $('#createGenreForm');
   4:   
   5:      if (createGenreForm.valid()) {
   6:   
   7:          $.post(createGenreForm.attr('action'), createGenreForm.serialize(), function (data) {
   8:   
   9:              if (data.Error != '') {
  10:   
  11:                  alert(data.Error);
  12:   
  13:              }
  14:   
  15:              else {
  16:   
  17:                  // Add the new genre to the dropdown list and select it
  18:   
  19:                  $('#GenreId').append(
  20:   
  21:                          $('<option></option>')
  22:   
  23:                              .val(data.Genre.GenreId)
  24:   
  25:                              .html(data.Genre.Name)
  26:   
  27:                              .prop('selected', true)  // Selects the new Genre in the DropDown LB
  28:   
  29:                      );
  30:   
  31:                  $('#genreDialog').dialog('close');
  32:   
  33:              }
  34:   
  35:          });
  36:   
  37:      }
  38:   
  39:  },

The var createGenreForm is selected from the createGenreForm ID. The createGenreForm ID was set in the following code found in the *Views\_CreateGenre.cshtml* file.

[!code-cshtmlMain]

   1:  @model MvcMusicStore.Models.Genre
   2:   
   3:  @using (Html.BeginForm("Create", "Genre", FormMethod.Post, new { id = "createGenreForm" }))
   4:   
   5:  {
   6:   
   7:      @*Omitted for clarity.*@
   8:   
   9:  }

The Html.BeginForm helper overload used in the *Views\_CreateGenre.cshtml* file generates HTML with an action attribute containing the URL to submit the form. You can see this by displaying the create album page in a browser and selecting show source in the browser. The following markup shows the generated HTML containing the form tag.

[!code-htmlMain]

   1:  <form action="/StoreManager/Create" method="post">

The jQuery $.post line makes an AJAX call to the action attribute (/StoreManager/Create) and passes in the data from the Create Genre dialog box. The data consists of the name for the new genre and an optional description. If the AJAX call is successful, the new genre name and value are added to the Select markup, and the new genre is set to the selected value. Because this is dynamically generated markup, you can’t see the new select option by viewing the source in the browser. You can see the new HTML with the IE 9 F12 developer tools. To view the new select option, in Internet Explorer 9, hit the F12 key to start the F12 developer tools. Navigate to the Create page and add a new genre so the new genre is selected in the genre select list. In the F12 developer tools:

  1. Select the HTML tab.
  2. Hit the refresh icon.
  3. In the search box, enter GenreID.
  4. Using the next icon,

    navigate to the following select tag:

    [!code-htmlMain]
       1:  <select name="GenreId" id="GenreId" >
  5. Expand the last option value.

The following code in the Scripts.js file shows the how the Add New Genre button gets connected to the click event, and how the Add New Genre dialog box is created.

[!code-javascriptMain]

   1:  $('#genreAddLink').click(function () {
   2:   
   3:      var createFormUrl = $(this).attr('href');  
   4:   
   5:      $('#genreDialog').html('')
   6:   
   7:      .load(createFormUrl, function () {  
   8:   
   9:          // The createGenreForm is loaded on the fly using jQuery load. 
  10:   
  11:          // In order to have client validation working it is necessary to tell the 
  12:   
  13:          // jQuery.validator to parse the newly added content
  14:   
  15:          jQuery.validator.unobtrusive.parse('#createGenreForm');
  16:   
  17:          $('#genreDialog').dialog('open');
  18:   
  19:      });
  20:   
  21:      return false;
  22:   
  23:  });

The first line creates a click function attached to the Add New Genre button. The following markup from the Views\_ChooseGenre.cshtml file shows how the Add New Genre button is created:

[!code-cshtmlMain]

   1:  <a class="button" href="@Url.Content("~/Genre/Create")" 
   2:   
   3:      id="genreAddLink">Add New Genre</a>

The load method creates and opens the Add Genre dialog and calls the jQuery parse method so client validation occurs on data entered in the dialog.

In this section you have learned how to create a dialog that can be used to add new category data to a select list. You can follow the same procedure to create UI to add a new artist to the artist select list. This tutorial has given an overview of working with the ASP.NET MVC HTML helper DropDownList. For additional information on working with the DropDownList, see the addition references section below. Please let us know if this tutorial has been helpful.

Rick.Anderson[at]Microsoft.com

Additional References

Contributors

Reviewers

Previous



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/adding-a-new-category-to-the-dropdownlist-using-jquery-ui.htm
< THANKS ME>