2017-06-29 48 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节。