0

2 つの異なるドロップダウン リストがあり、それぞれに適切な情報を返す select ステートメントがあります。いくつかの問題が発生しています:

  • selectステートメントからの結果をどの形式で適切に返す必要がありますか?

  • 最初のドロップダウン リストで選択されたアイテムの ID に基づいて、2 番目のドロップダウン リストの select ステートメントを制御しますか。

サービス層: 1 番目のドロップダウン リスト

public IEnumerable<ContentVw> GetSections()
        {
            using (var db = my connection info)
            {
                var sections = (from e in db.Table1
    join re in db.RootTables re
    on e.ID equals re.Table1ID
    where re.ChairID == 1
    select new { e.DisplayName, e.ID, e.GUID };
                return sections;
            }
        }

エラー: IQueryable 匿名を ...ContentVw に変換できません

2 番目のドロップダウン リスト

public IEnumerable<ContentVw> GetContent(int itemId) //itemId = first dropdown list selection
        {
            using (var db = my connection info)
            {
                var content = (from e in db.Table1 join em in db.TableToTableMaps on e.ID equals em.KnowsTableID where em.TableID == itemId select new { e.DisplayName, e.ID, e.GUID });
            }
        }

コンテンツビュー:

public partial class ContentVw
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public Guid GUID { get; set; }
    }

コントローラ

public ActionResult ContentManage()
        {
            var _sections = new ContentService().GetSections().ToList();
            ViewBag.SectionsDropdown = _sections;
            return View();
        }
4

2 に答える 2

1

使用する:

    public IEnumerable<ContentVw> GetSections()
    {
        using (var db = my connection info)
        {
            return (from e in db.Table1
                            join re in db.RootTables re
                            on e.ID equals re.Table1ID
                            where re.ChairID == 1                                
                            select new ContentVw { Name = e.DisplayName, // here you get ContentVw  objects
                                                   Id = e.ID,
                                                   GUID = e.GUID }).ToList();

        }
    }

コントローラ:

public ActionResult ContentManage()
        {
            var _sections = new ContentService().GetSections();
            // ViewBag.SectionsDropdown = _sections; // i prefare to pass data im model
            return View(_sections);
        }

意見:

@model IEnumerable<ContentVw>

@{ // populate list of <SelectListItem> for helper
   var listItems = Model.Select(x => new SelectListItem {
        Text = Name,
        Value = Id.ToString()
   }).ToList();
}

@Html.DropDownList("List", listItems)
于 2013-07-29T18:37:18.603 に答える