0

I am new to web app development. I am using ASP.NET MVC6 EF7 to create simple app that provides a set of questions to the user, these questions are served on Pages. For reuse, the pages and questions need to have a many-many relationship.

The model (I am developing code-first) is setup as described in the responses to the other posts like this one or 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 code was used as a starting point.

I would like to link multiple questions to a page when I set it up. In the PagesController:

ViewData["QuestionsID"] = new MultiSelectList(_context.Question, "QID", "Text");

and in the Razor file i create the listbox as follow:

@Html.ListBox("QuestionsID", null, htmlAttributes: new { @class = "form-control" })

So far so good. Problem is I am not sure how to return the newly selected questions from the view and correctly populate the PQJoin table. What is the recommended approach?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
MarkM
  • 11
  • 2

1 Answers1

0

You need a link from Page to PQJoin to Question.

Add this to Page and Question class:

public IList<PQJoin> PQJoins {get;set;}
alanh
  • 1,153
  • 10
  • 14