question

EB-8703 avatar image
0 Votes"
EB-8703 asked YihuiSun-MSFT answered

DDL require selection

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; }



dotnet-aspnet-core-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YihuiSun-MSFT avatar image
0 Votes"
YihuiSun-MSFT answered

Hi @EB-8703,

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

  1. Controller

  2. 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


controller.txt (1.2 KiB)
view.txt (752 B)
result.gif (130.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 edited
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.