Asp.net mvc 4 如何为MVC3中列表项的未知计数创建视图

Asp.net mvc 4 如何为MVC3中列表项的未知计数创建视图,asp.net-mvc-4,Asp.net Mvc 4,我正在开发一个新的问答网站。我不知道如何保存多个选项。 我不知道一个问题有什么选择。 我的模型是 public int QuestionID { get; set; } public string QuestionName { get; set; } public List<Options> Options { get; set; } public string Answer { get; set; } public

我正在开发一个新的问答网站。我不知道如何保存多个选项。 我不知道一个问题有什么选择。 我的模型是

 public int QuestionID { get; set; }
        public string QuestionName { get; set; }
        public List<Options> Options { get; set; }
        public string Answer { get; set; }
        public string CreateDate { get; set; }
        public bool IsDelete { get; set; }
public int QuestionID{get;set;}
公共字符串QuestionName{get;set;}
公共列表选项{get;set;}
公共字符串答案{get;set;}
公共字符串CreateDate{get;set;}
公共bool IsDelete{get;set;}
您能帮助如何在数据库中保存多个选项吗


你能帮我一下吗?

我不知道你说的“选项”到底是什么意思,但是:

在你的问题课上讨论这个问题:

public virtual ICollection<Option> Options { get; set; } // which will be used to access your Question's options appointments.
在选项类中,如果您希望在问题和选项之间建立一对多关系(“一个问题可能有许多选项,但一个选项可能只属于一个问题”),或

  • 然后在你看来:

    @DropDownList(“Answer”,String.Empty)

  • 同样,我不完全理解您的数据模型,但我相信如果您的“答案”是选项之一,那么您应该使用
    public int AnswerID{get;set;}
    而不是
    public int AnswerID{get;set;}//来保存所选选项的id。


我希望这能帮助你走得更远。还可以查看有关下拉列表的stackoverflow问题(例如)

您好,谢谢您的建议,但我的问题是使用模型绑定时,我没有下拉自动生成的视图方法写入单击操作添加视图选择模型,然后单击添加视图所有字段都添加到自动生成的视图,但我没有将选项字段作为下拉列表
public int QuestionID { get; set; }             // The ID of the question that this option belong to
public virtual Question Question { get; set; }  // The question that this option belong to
public virtual ICollection<Question> Questions { get; set; }
@for (int i = 0; i < Model.Option.ToList().Count(); i++){
   @Html.EditorFor(m => m.Option.ToList()[i]);  // Create an EditorFor template for your "question" and use it here. 
}
ViewBag.Answer= new SelectList(question.Options, "Name_of_yuour_Option_id_field", "Name_of_a_field_that_describes_your_Option");