2017-06-29 79 views
-1

我是新的Web,真的卡住了。我需要使用ASP.NET和Entity Framework來製作QuizSystem。我第一次使用DATABSE,我有這樣的表ASP.NET實體框架製作測驗

Qustion Table

而且

Variant Table

我努力使創建視圖的問題,但問題是問使用一些已經取得變量作爲答案我只需要從下拉列表中選擇它。

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "Text,AnswerID,ChapterID")] QuestionVariant questionVariant) 
    { 
     if (ModelState.IsValid) 
     { 
      db.QuestionVariants.Add(questionVariant); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.ChapterID = new SelectList(db.Chapters, "ChapterID", "ChapterName", questionVariant.ChapterID); 
     ViewBag.AnswerID = new SelectList(db.Variants, "VariantID", "Text", questionVariant.AnswerID); 
     return View(questionVariant); 
    } 

是否可以在同一視圖中創建問題和變體?如果是這樣,控制器和視圖應該如何顯示?不要只把-1,幫助我一些建議或任何有助於解決這個問題。

+0

QuestionVariants是指問題的答案? – Nico

+0

@Nico是的問題可能的答案 –

+0

你在問什麼?當然,你可以把它們放在同一個視圖中......你已經展示了你的視圖發送到的POST方法處理程序? – Milney

回答

0

我認爲你的餐桌設計需要一些調整。試試你的問題model.AnswerId改爲=>

public virtual ICollection<QuestionVariants> QuestionVariants{ get; set; } 

添加問題屬性QuestionVariant和應用ForeignKey的屬性了這一點。

[ForeignKey("QuestionId")]  
public Question Question { get; set; } 

此外,您可以閱讀「ForeignKey的」和「一對多」的this tutorial節。