填充从linq到sql语句的下拉列表

时间:2013-07-29 18:32:28

标签: c# asp.net-mvc linq-to-sql

我有两个不同的下拉列表,每个下拉列表都有一个select语句来返回正确的信息。遇到几个问题:

  • 我应该将select语句的结果正确返回为什么格式?

  • 我是否根据第一个下拉列表中所选项目的ID控制第二个下拉列表的select语句。

服务层: 第一个下拉列表

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

第二次下拉列表

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 });
            }
        }

ContentVw:

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();
        }

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

您将返回匿名对象而不是ContentVw

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 ContentVw
                           {
                               Name = e.DisplayName,
                               Id = e.ID,
                               GUID = e.GUID
                           };

        return sections;
    }
}

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 ContentVw
                           {
                               Name = e.DisplayName,
                               Id = e.ID,
                               GUID = e.GUID
                           });

        return content;
    }
}