Populating a SelectList with Parent/Child Tables

时间:2015-10-30 23:29:02

标签: asp.net-mvc selectlistitem

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.

1 个答案:

答案 0 :(得分:1)

您可以使用linq .Join()并将结果投影到匿名对象。假设var topics = db.ODSessionTopics;var types = db.ODSessionTypes;,那么查询将是

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

将输出

{ 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" }

并创建SelectList

ViewBag.ODSessionTopicID = new SelectList(query, "ID", "Description", "Group", oDSession.ODSessionTopicID)

附注:建议您使用强类型的html助手来生成下拉列表。您的ViewBag属性不应与绑定的属性相同。相反应该是(比方说)

ViewBag.TopicList = new SelectList(query, "ID", "Description", "Group", null)

并在视图中

@Html.DropDownListFor(m => m.ODSessionTopicID, (SelectList)ViewBag.TopicList, "-- Session Type --", new { @class = "form-control" })