locked
Dependendent droppowns not working correct after Contract resolver RRS feed

  • Question

  • User-183185495 posted

    I have a very strange error when it comes to my dropdowns sometimes they would appear fine but sometimes they come up saying the values are undefined.

    Below is my javascript

    <script>
        $(function () {
            $("#ddCat").change(function () {
                $.getJSON("/MISObjects/GetEnforcementTypes", { cid: $("#ddCat").val() }, function (d) {
                    var row = "";
                    $("#ddType").empty();
                    $.each(d, function (i, v) {
                        row += "<option value=" + v.Value + ">" + v.Text + "</option>";
                    });
                    $("#ddType").html(row);
                })
            })
            $("#ddType").change(function () {
                $.getJSON("/MISObjects/GetEnforcementsItem", { Sid: $("#ddType").val() }, function (d) {
                    var row = "";
                    $("#dditem").empty();
                    $.each(d, function (i, v) {
                        row += "<option value=" + v.Value + ">" + v.Text + "</option>";
                    });
                    $("#dditem").html(row);
                })
            })
        })
    
    </script>

    This is my dropdowns

      <div class="form-group col-md-2">
        <select asp-for="Enf_Cat" id="ddCat" class=" form-control" data-style="btn-new" asp-items="ViewBag.EnforcementCats">
            <option>Category</option>
        </select>
    </div>
    
    <div class="form-group col-md-2">
        <select asp-for="Enf_Type" id="ddType" class=" form-control" data-style="btn-new">
            <option>Type</option>
        </select>
    </div>
    
    <div class="form-group col-md-2">
        <select asp-for="Enf_Item" id="dditem" class=" form-control" data-style="btn-new">
            <option>Item</option>
        </select>
    </div>
    <div class="form-group col-md-2">
        <select asp-for="Priority" id="ddpriotity" asp-items="ViewBag.Prioritys" class=" form-control" data-style="btn-new">
        </select>
      </div>
    </div>

    My Code Behind Calls

    public JsonResult GetEnforcementTypes(int cid) {
            List<SelectListItem> list = new List<SelectListItem>();
            var items = _context.EnfTypes.Where(w => w.CategoryId == cid).ToList();
            foreach (var item in items) {
                list.Add(new SelectListItem { Text = item.Description.ToString(), Value = item.TypeId.ToString() });
            }
            return Json(list);
        }
    
    
    public JsonResult GetEnforcementsItem(int Sid) {
            List<SelectListItem> list = new List<SelectListItem>();
            var items = _context.EnfItem.Where(w => w.SubTypeId == Sid).ToList();
            foreach (var item in items) {
                list.Add(new SelectListItem { Text = item.Description.ToString(), Value = item.Id.ToString() });
            }
            return Json(list);
    }

    But when I Select the first dropdown sometimes its saying undefined as if cannot find it this was working before when i had the following not in start-up. But i need it for my telerik controls to work properly.

    .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
    

    For example the second dropdown which does have data related to it in behind and can see it in the objects when debug is showing undefind.

    Saturday, August 22, 2020 7:54 PM

All replies

  • User-474980206 posted

    You should use the browserS network debugger to verify, but most likely the value and text entries in the json are lowercase.

    Saturday, August 22, 2020 9:09 PM
  • User1535942433 posted

    Hi roguenidb,

    Accroding to your description and codes,as far as I think,only you change the ddCat dropdownlist's value, the change event will be excute and the ddType dropdownlist will be filled.

    Do you have binded value on dropdownlist?Also you could use on change event.

    Just like this:

    <script src="~/Scripts/jquery-3.0.0.min.js"></script>
        <script>
            $(function () {
                $("#ddCat").on("change", function (){
                    $.getJSON("/DropdownCore/GetEnforcementTypes", { cid: $("#ddCat").val() }, function (d) {
                        var row = "";
                        $("#ddType").empty();
                        $.each(d, function (i, v) {
                            row += "<option value=" + v.Value + ">" + v.Text + "</option>";
                        });
                        $("#ddType").html(row);
                    })
                })
                $("#ddType").change(function () {
                    $.getJSON("/MISObjects/GetEnforcementsItem", { Sid: $("#ddType").val() }, function (d) {
                        var row = "";
                        $("#dditem").empty();
                        $.each(d, function (i, v) {
                            row += "<option value=" + v.Value + ">" + v.Text + "</option>";
                        });
                        $("#dditem").html(row);
                    })
                })
            })
        </script>
    
    
     <div class="form-group col-md-2">
            <select asp-for="Enf_Cat" id="ddCat" class=" form-control" data-style="btn-new" asp-items="ViewBag.EnforcementCats">
                <option>Category</option>
                <option>1</option>
            </select>
        </div>
    
        <div class="form-group col-md-2">
            <select asp-for="Enf_Type" id="ddType" class=" form-control" data-style="btn-new">
                <option>Type</option>
            </select>
        </div>
    
        <div class="form-group col-md-2">
            <select asp-for="Enf_Item" id="dditem" class=" form-control" data-style="btn-new">
                <option>Item</option>
            </select>
        </div>
        <div class="form-group col-md-2">
            <select asp-for="Priority" id="ddpriotity" asp-items="ViewBag.Prioritys" class=" form-control" data-style="btn-new"></select>
        </div>

    Result:

    If you have other problems,you could tell us more details to us.

    Best regards,

    Yijing Sun

    Monday, August 24, 2020 8:06 AM