return Json函数没有更新我的下拉列表

时间:2014-07-04 11:45:45

标签: javascript jquery asp.net asp.net-mvc json

我有一个带有填充下拉列表的视图,我添加了一个按钮,因此用户可以在jquery模式中添加新的下拉列表值,因此当用户单击以添加新的下拉列表值时,我想刷新下拉列表但是目前没有任何更新。

这是我的下拉列表视图

@model Communique.Models.PersonSupplierDetailsModel
@using Boxharry.Extensions;
@using Communique.Models;

@{
    List<SelectListItem> languages = ViewData["languages"] as List<SelectListItem>;

    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_LayoutApplicationModal.cshtml";

    if(Model.Languages == null)
    {
        Model.Languages = new List<LanguageModel>();
    }
}

<section class="page-content tabs-panel-content">          
    <div class="workspace-column ">
        <div class="modal-content person-supplier-details">
            <div class="sub-menu">
                <a class="create language-modal" href="#">Add a new Language</a>
            </div>
            <div class="modal-workspace-contents modal-form">
                @using (Html.BeginForm("SupplierDetails", "People"))
                {
                        @for(int i = 0; i < 3; i++)
                        {
                            var language = Model.Languages.ElementAtOrDefault(i);
                            if (language == null) { Model.Languages.Add(new LanguageModel()); }

                            @Html.HiddenFor(x => x.Languages[i].Id)
                            @Html.WrappedDropDownListFor(x => x.Languages[i].LanguageFrom, languages, new { @class = "form-item-container dropdownlist supplier-languages" }, "", "Please select...")
                            @Html.WrappedDropDownListFor(x => x.Languages[i].LanguageTo, languages, new { @class = "form-item-container dropdownlist supplier-languages" }, "", "Please select...")
                        }

                }
            </div>
        </div>
    </div>
</section>

这是控制器功能,称为

[HttpGet]
        public ActionResult GetLanguages()
        {
            CommuniqueWrapper communiqueWrapper = new CommuniqueWrapper();

            var languages = communiqueWrapper.GetTags("Languages", true);

            return Json(languages, JsonRequestBehavior.AllowGet);
        }

这是应该刷新下拉列表的视图

@{
    Layout = null;
}

<script>
    $.getJSON("@Url.Action("GetLanguages")", function (data) {
        debugger;
        var $languages = $('.supplier-languages');
        $languages.children().filter(function () {
            return $(this).attr('value');
        }).remove();
        $.each(data, function () {
            debugger;
            var option = $('<option />', {
                text: this.Text,
                value: this.Value
            });

            $languages.append(option);
        });
    });
    jSnap.modals.removeModal();
</script>

有人能看出为什么我的下拉列表不令人耳目一新吗?

1 个答案:

答案 0 :(得分:1)

试试这个

var select = $("#dropdownid");
select.empty();
  $.each(data, function (index, itemData) {
    select.append($('<option/>', {
      value: itemData.Value,
      text: itemData.Text
     }));
  });