Share via

DDL require selection

EB 1 Reputation point
2021-06-21T15:02:41.097+00:00

I am trying to require a selection on a DDL but not sure how to do it. I would like to display ---Select--- and make the user pick the correct selection. Here is my view, controller and model code. Could someone tell me what I am doing wrong?

Thanks!

View:

$.ajax({
    type: 'POST',
    url: '@Url.Action("GetCategory")', // we are calling json method

    dataType: 'json',

    data: { id: $("#RequestType").val() },

    success: function (category) {
        $.each(category, function (i, category) {
            $("#Catagory").append('<option value="' + category.Value + '">' +
                category.Text + '</option>');

        });

Controller:

 public JsonResult GetCategory(string id)
{
List<SelectListItem> category = new List<SelectListItem>();
switch (id)
{
    case "1": 
        category.Add(new SelectListItem { Text = "N/A", Value = "N/A" });

        break;
    case "2":
        category.Add(new SelectListItem { Text = "N/A", Value = "N/A" });

        break;
    case "3":
        category.Add(new SelectListItem { Text = "N/A", Value = "N/A" });
        break;
    case "4": 
        category.Add(new SelectListItem { Text = "---Select---", Value = null });
        category.Add(new SelectListItem { Text = "Address", Value = "Address" });
        category.Add(new SelectListItem { Text = "Closing", Value = "Closing" });

}
return Json(new SelectList(category, "Value", "Text"));

Model:

[Required(ErrorMessage = "Category Required.")]
[DisplayName("Category")]
public string Category { get; set; }
Developer technologies | ASP.NET Core | Other
0 comments No comments

3 answers

Sort by: Most helpful
  1. Yihui Sun-MSFT 806 Reputation points
    2021-06-22T09:42:13.94+00:00

    Hi @EB ,

    I tested the code you provided, and I found two places that need to be modified:

    1. The last "case" lacks a break.
      -  
           case "4":  
              category.Add(new SelectListItem { Text = "---Select---", Value = null });  
              category.Add(new SelectListItem { Text = "Address", Value = "Address" });  
              category.Add(new SelectListItem { Text = "Closing", Value = "Closing" });  
              break;  
      
    2. You should first empty the html of $("#Catagory") and then fill it.
       -    
      
      $("#Catagory").empty();
      $.each(category, function (i, category) {
      $("#Catagory").append('<option value="' + category.Value + '">' +
      category.Text + '</option>');
      });
      Code
    3. Controller
    4. View

    Result
    108133-result.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best Regards,
    YihuiSun

    Was this answer helpful?

    0 comments No comments

  2. Duane Arnold 3,216 Reputation points
    2021-06-21T19:03:59.037+00:00

    Was this answer helpful?

    0 comments No comments

  3. Michael Taylor 61,226 Reputation points
    2021-06-21T15:15:30.69+00:00

    Seems like that should work as the required will look for null and you're setting your select option to null. However in your binding code you are using the expression <value which means your actual option would be an empty string. Since empty string does not match the RequiredAttribute requirements it will see it as valid. The easiest solution is to probably add the AllowEmptyString property on the attribute to false. This would cause it to see both null and empty string as not meeting the requirements.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.