0

i have two different dropdown lists and each have a select statement to return proper information. Running into a few issues:

  • what format should i properly return the results from select statement as?

  • do i control the select statement of the second dropdown list based on the id from the selected item in the first dropdown list.

Service layer: 1st dropdown list

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

error: cannot convert IQueryable anonymous to ...ContentVw

2nd dropdownList

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

Controller

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 回答
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;
    }
}
于 2013-07-29T18:38:42.253 回答