locked
cascade dropdown shows the error something went wrong , Please help RRS feed

  • Question

  • User-1355965324 posted

    In Product create view I have  a cascadedropdpown to select the available sized for the selected uniform type. For example I have selected the uniformtype 'Shirt', the corresponding size should be listed , If I select uniformtype Boots, the corresponding size of the selected Boots should be listed in the drop down of size column

    I have the following table 

    public class UniformType
        {
            [Key]
            public string Catagory { get; set; }
        }
    
    
        public class SizeModel
        {
            [Key]
            public int SizeID { get; set; }
    
            [DisplayName("Size")]
            [DefaultValue("")]
            [Required]
            public string Size { get; set; }
    
            [DefaultValue("")]
            [Required]
            public string Description { get; set; }
            [Required]
            public string Catagory { get; set; }
    
            public virtual UniformType UniType { get; set; }
        }
    
     public ProductModel()
            {
                Rate = 0.00M;
                Description = string.Empty;
            }
    
            [Key]
            public int ProductID { get; set; }
            
            [Required]
            public string Catagory { get; set; }
    
     
            [DisplayName("Size")]
            [DefaultValue(0)]
            public int SizeID { get; set; }  // Here  the selected size of Catagory of unitform type should be listed
    
          
    
    

    repository class

    public IEnumerable<SelectListItem> GetSizeforCatagory(string catagory)
            {
                //IEnumerable<SelectListItem> sz;
                List<SelectListItem> sz = (from siz in goContext.goSize
                                             where siz.Catagory == catagory
                                             orderby siz.Size, siz.Description
                                             select new SelectListItem
                                             {
                                                 Value = siz.SizeID.ToString(),
                                                 Text = siz.Size + " - " + siz.Description
                                             }).ToList<SelectListItem>();
                sz.Insert(0, new SelectListItem
                {
                    Value = "0",
                    Text = "-- Select --"
                });
    
               
                return sz;
            }

    ProductController 

    public async Task<IActionResult> Create ()
            {
                ProductModel product = new ProductModel();
                ViewBag.Catagory = _unitOfWork.UniformType.GetUniformTypeDropDown();
                
    }

    Product View

     <div class="col-md-6">
                                        <div class="form-group">
                                            <label asp-for="Catagory" class="control-label control-label-left col-sm-2 text-danger"></label>
                                            <div class="controls col-sm-9">
                                                <select asp-for="Catagory" class="form-control" asp-items="@ViewBag.Catagory" data-role="select"
                                                        onchange="FillSizeDropdown()" id="Catagory"></select>
                                                <span asp-validation-for="Catagory" class="text-danger">@ViewBag.ErrorCatagoryType</span>
                                            </div>
                                        </div>
                                    </div>
    
    
    
     <div class="col-md-6">
                                        <div class="form-group">
                                            <label asp-for="SizeID" class="control-label control-label-left col-sm-2 text-danger"></label>
                                            <div class="controls col-sm-9">
                                                @*<input asp-for="SizeID" class="form-control" />*@
                                                <select id="sizecolumn" class="form-control" asp-for="SizeID" asp-items="@ViewBag.Size" data-role="select"></select>
                                                <span asp-validation-for="SizeID" class="text-danger"></span> // Here I want bring the size of the selected dropdown
                                            </div>
                                        </div>
                                    </div>
    
    <Script>
    <script type="text/javascript">
        $(document).ready(function () {
            KeyHandler();
            $('#SupplierID').focus();
             ShowResultModalPopup('@ViewBag.Result');
        });
    
     function FillSizeDropdown() {
              debugger;
              var catagory = $('#Catagory option:selected');
              $.get("/Product/GetSizebyCatagory", { catagory: $("#Catagory").val() }, function (data) {
                  $("#sizecolumn").empty();
                  $.each(data, function (index, row) {
                      debugger;
                      $("#sizecolumn").append("<option value='" + row.Value + "'>" + row.Text + "</option>")
                  });
              }
    
    </Script>

    In Product Controller 

    public async Task<IActionResult> Create ()
            {
                ProductModel product = new ProductModel();
                ViewBag.Catagory = _unitOfWork.UniformType.GetUniformTypeDropDown();
    }
    
    
    }
    public JsonResult GetSizebyCatagory(string catagory)
            {
                IEnumerable<SelectListItem> sz;
                sz = _unitOfWork.Size.GetSizeforCatagory(catagory);
                return Json(new SelectList(sz)); //  Here the function returns the value , but it is not showing in the view drop down
    
            }
    
    
    
    

    Value = "0", Text = "-- Select --"

    The row column  Value is stored with each size of the selected catagory

    the row column text is stored with Description of the size

    But it is not coming  in the dropdown of the sizeID in Product View. 'Something Went Wrong' Error message is displayed

    Please help

    Saturday, December 28, 2019 4:19 PM

Answers

  • User-854763662 posted

    Hi polachan ,

    There are two places in your code that need to be modified as shown:

    JQuery

    function FillSizeDropdown() {
            debugger;
            var catagory = $('#Catagory option:selected');
            console.log(catagory);
            $.getJSON("/Home/GetSizebyCatagory", { catagory: $("#Catagory").val() }, function (data) {
                $("#sizecolumn").empty();
                $.each(data, function (index, row) {
                    debugger;
                    $("#sizecolumn").append("<option value='" + row.value + "'>" + row.text + "</option>")
                });
            });
        }

    GetSizebyCatagory , specify the dataValueField and dataTextField of SelectList

    public JsonResult GetSizebyCatagory(string catagory)
    {
                IEnumerable<SelectListItem> sz;
                sz = _unitOfWork.GetSizeforCatagory(catagory);
                return Json(new SelectList(sz,"Value", "Text")); 
    }

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2019 5:37 AM