简体   繁体   English

为什么我的下拉列表项不可见?

[英]Why my drop down list items are invisible?

I am trying to make 2 cascading drop down lists. 我正在尝试制作2个级联的下拉列表。

First one works fine, you pick an item but when you go to the second drop down list, you can actually see correct number of spaces generating according to the items in the database but not the items themselves! 第一个工作正常,选择一个项目,但是当转到第二个下拉列表时,实际上可以看到根据数据库中的项目生成的正确数量的空格,而不是根据项目本身生成的!

Its like they are invisible! 就像它们是隐形的!

Can you please advise? 你能给些建议么?

My View : 我的观点 :

@using (Html.BeginForm("Browse", "Bookings",  new { id = "TypeItemFormID", data_itemsListAction = @Url.Action("ItemsList") }))
{
<fieldset>
    <legend> Type/Item</legend>
    @Html.DropDownList("department", ViewBag.ItemTypesList as SelectList, "Select a Type", new {id="ItemTypeID"})
    <div id="ItemsDivId">
        <label for="Items">Items </label>
        <select id="ItemsID" name="Items"></select>
    </div>
    <p>
        <input type ="submit" value="Submit" id="SubmitID" />
    </p>
 </fieldset>
}


    <script type="text/javascript">

        $('#ItemTypeID').on('change', function () {
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetItemTypeForm")',
            data: { itemTypeId: $('#ItemTypeID').val() },
            success: function (results) {
                var options = $('#ItemsID');
                options.empty();
                options.append($('<option />').val(null).text("- Select an Item -"));
                $.each(results, function () {
                    options.append($('<option />').val(this.ItemsID).text(this.Value));
                });
            }
        });
    });
</script>

My Controller : 我的控制器:

[HttpPost]
        public JsonResult GetItemTypeForm(string itemTypeId)
        {
            //pseudo code
            var data = from s in db.Items
                       where s.ItemType.ItemTypeName == itemTypeId
                       select s.ItemName;

            return Json(data);
        }

You have the initial problem I mentioned in my comment, but it appears from your comments that your member names probably do not match either. 您有我在评论中提到的第一个问题,但从您的评论看来,您的会员名可能也不匹配。

This may not be exact, as we do not know all your data/member names, but you may want something like this (using an anonymous type to return the shape of data you expect): 这可能并不准确,因为我们不知道您的所有数据/成员名称,但是您可能需要这样的方法(使用匿名类型返回您期望的数据形状):

    [HttpPost]
    public JsonResult GetItemTypeForm(string itemTypeId)
    {
        //pseudo code
        var data = from s in db.Items
                   where s.ItemType.ItemTypeName == itemTypeId
                   select new { Value = s.ItemName, ItemsID = s.ItemId };

        return Json(data);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM