【发布时间】:2016-09-10 01:30:11
【问题描述】:
我是网络应用程序开发的新手。我正在使用 ASP.NET MVC6 EF7 创建简单的应用程序,该应用程序向用户提供一组问题,这些问题在 Pages 上提供。为了重用,页面和问题需要具有多对多关系。
模型(我正在开发代码优先)的设置如对 this one 或 this one 等其他帖子的回复中所述。
// Data Model
public class Page
{
public int PageID { get; set; } // Key
}
public class Question
{
Public int QID { get; set; } // Key
Public string Text { get; set; } // The question
}
public class PQJoin
{
public int PageID { get; set; }
public virtual Page page { get; set; }
public int QID { get; set; }
public virtual Question question { get; set; }
}
EF Scaffolded CRUD 代码被用作起点。
我想在设置页面时将多个问题链接到一个页面。在 PagesController 中:
ViewData["QuestionsID"] = new MultiSelectList(_context.Question, "QID", "Text");
在 Razor 文件中,我按如下方式创建列表框:
@Html.ListBox("QuestionsID", null, htmlAttributes: new { @class = "form-control" })
到目前为止一切顺利。问题是我不确定如何从视图中返回新选择的问题并正确填充PQJoin 表。推荐的方法是什么?
【问题讨论】:
标签: asp.net many-to-many asp.net-core-mvc entity-framework-core