用父/子表填充SelectList

时间:2020-11-26 01:02:07

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:

我正在尝试填充一个Html。它在MVC。我遇到的问题是,我有一个名为“odsessiontopic”的子表,该表具有“ODSessionType”表的外键(ID),其中包含主题的标题。这是我的模型:

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:

ViewBag.ODSessionTopicID = new SelectList(db.ODSessionTopics, "ID", "Description", "ODSessionTypeID", oDSession.ODSessionTopicID);

Here is the OD Session Topic data:

以下是OD会话主题数据:

    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:

这里是OD会话类型数据:

    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.

基本上,它在ODSessionTopic表中获取ODSessionTypeID外键并按该值进行分组。我需要它从ODSessionType表中获取描述。这是可能的吗?主题和类型都是可编辑的,并附带了CRUD逻辑,这就是我最初是如何来到这个设计的。

1 个解决方案

#1


1  

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

您可以使用linq . join()并将结果投射到匿名对象。假设var topic = db. odsessiontopic;var类型= 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 };

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

并创建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)

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

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

#1


1  

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

您可以使用linq . join()并将结果投射到匿名对象。假设var topic = db. odsessiontopic;var类型= 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 };

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

并创建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)

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

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