Reputation: 2525
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();
}
Upvotes: 0
Views: 1432
Reputation: 5905
Use:
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();
}
}
Controller:
public ActionResult ContentManage()
{
var _sections = new ContentService().GetSections();
// ViewBag.SectionsDropdown = _sections; // i prefare to pass data im model
return View(_sections);
}
View:
@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)
Upvotes: 1
Reputation: 21275
You're returning an anonymous object instead of your 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;
}
}
Upvotes: 0