Reputation: 85
I am trying to populate an Html.DropDownList in MVC. The problem I'm having is that I have a child table called "ODSessionTopics" that has a foreign key (ID) to the "ODSessionType" table which contain the headers for the topics. Here are my models:
public class ODSessionTopic
{
public int ID { get; set; }
public string Description { get; set; }
// foreign key
public int ODSessionTypeID { get; set; }
// navigation property
public virtual ODSessionType ODSessionType { get; set; }
}
public class ODSessionType
{
public int ODSessionTypeID { get; set; }
public string Description { get; set; }
// navigation property
public virtual ICollection<ODSessionTopic> ODSessionTopics { get; set; }
}
I use the following code to populate the ViewBag:
ViewBag.ODSessionTopicID = new SelectList(db.ODSessionTopics, "ID", "Description", "ODSessionTypeID", oDSession.ODSessionTopicID);
Here is the OD Session Topic data:
ID Description ODSessionTypeID
---------------------------------------------------
1 Internal Marketing 1
2 Team Development 1
3 Department Retreat 2
4 Community Service 2
Here is the OD Session Type data:
ODSessionTypeID Description
-------------------------------------
1 Plan
2 Action
These are my results:
1
Internal Marketing
Team Development
2
Department Retreat
Community Services
Here are the results I am trying to achieve:
Plans
Internal Marketing
Team Development
Actions
Department Retreat
Community Services
The view code is
@Html.DropDownList("ODSessionTopicID", null, "-- Session Type --", htmlAttributes: new { @class = "form-control" })
Basically, it's grabbing the ODSessionTypeID foreign key in the ODSessionTopic table and grouping by that value. What I need it to do is grab the description from the ODSessionType table. Is this possible? Both the topics and the types are editable and have CRUD logic attached to them which is how I arrived to this design in the first place.
Upvotes: 2
Views: 2483
Reputation:
You can use a linq .Join()
and project the results to an anonymous object. Assuming var topics = db.ODSessionTopics;
and var types = db.ODSessionTypes;
then the query would be
var query = from topic in topics
join type in types on topic.ODSessionTypeID equals type.ODSessionTypeID
select new { Group = type.Description, ID = topic.ID, Description = topic.Description };
which will output
{ Group: "Plan", ID: 1, Description: "Internal Marketing" }
{ Group: "Plan", ID: 2, Description: "Team Development" }
{ Group: "Action", ID: 3, Description: "Department Retreat" }
{ Group: "Action", ID: 4, Description: "Community Services" }
and to create the SelectList
ViewBag.ODSessionTopicID = new SelectList(query, "ID", "Description", "Group", oDSession.ODSessionTopicID)
Side note: Recommend you use the strongly typed html helpers to generate your dropdownlist. Your ViewBag
property should not be the same name as the property your binding to. Instead it should be (say)
ViewBag.TopicList = new SelectList(query, "ID", "Description", "Group", null)
and in the view
@Html.DropDownListFor(m => m.ODSessionTopicID, (SelectList)ViewBag.TopicList, "-- Session Type --", new { @class = "form-control" })
Upvotes: 1