下拉列表没有填写Jquery,Ajax

时间:2016-08-07 11:05:20

标签: jquery ajax asp.net-mvc drop-down-menu

我想通过jQuery,Ajax来填充一个drodown-list, 在第一个下拉列表中可见= false 当用户点击一个按钮时,它会出现,同时必须填充数据,

每件事都很好,但数据没有填满下拉列表 我利用这个链接来制作这段代码: link

我在javascript中给每个人发了一个提醒,所以我发现它传递了数据, 因为它迭代了10次(根据我在db中的数据),但为什么只是它没有显示数据????? 这是我的下拉列表

<input type="button" onclick="edit(@item.Barcode);" value="خرید" class="btn btn-success btn-sm">

<div class="pull-left" style="display:none" id="pnlSize">
  <label class="control-label">سایز:</label>
  <div class="product-quantity" id="drpSize">
    @Html.DropDownListFor(m => m.Size, new SelectList(Enumerable.Empty
    <SelectListItem>(), "ID", "Size"), "انتخاب همه", new { @class = "form-control" })
  </div>

</div>

这是我的Javascript:

< script >

  function edit(Barcode) {
    $.ajax({
      url: '@Url.Action("selectedItemChanged")',
      type: 'GET',
      data: {
        barcode: Barcode
      },
      success: function(result) {
        // when the AJAX succeeds refresh the dropdown list with
        // the JSON values returned from the controller action
        var selectedDeviceModel = $('#Size');
        selectedDeviceModel.empty();
        $.each(result, function(index, item) {
          selectedDeviceModel.append(
            $('<option/>', {
              value: item.value,
              text: item.text
            })
          );
        });
      }
    });
    $('#pnlSize').show('fast');
    var val = $('#drpSize :selected').text();
    if (val != 'انتخاب همه' && val != null) {
      location.href = '@Url.Action("AddToCart", "ShoppingCart")?Barcode=' + Barcode + '&size=' + $('#drpSize :selected').text();;
      //window.location.href = url.replace('Barcode', Barcode);
    }
  }
< /script>

这是我的控制器代码:

public JsonResult selectedItemChanged(string barcode)
{
    AdidasWebsiteEntities context = new AdidasWebsiteEntities();
    var itemsList = context.tblSiteSizes.Where(item => item.Barcode.Substring(0, item.Barcode.Length - 3) == barcode.Substring(0, barcode.Length - 3)).ToList();
    return Json(new[]
    {
        new {
            value=itemsList[0].ID,
            text=itemsList[0].Size
           } 

    });
}
希望有人花一些时间在这上面,我厌倦了测试和网上冲浪

1 个答案:

答案 0 :(得分:0)

如果其他人可能发生, 上面代码的问题是下拉列表的名称(大小) 我的意思(大小)的项目名称是相同的,

一旦我更改了Dropdown列表的名称,它就得到了修复