动态添加项目到视图中的列表

时间:2014-09-06 01:54:10

标签: asp.net-mvc dynamic

是否可以在视图中向列表中添加多个项目?假设我在下面有以下数据合同:

public class Question
{
    public int QuestionID { get; set; }
    public string Name { get; set; }
    public virtual List<AdditionalAnswer> AdditionalAnswers { get; set; }
}

public class AdditionalAnswer
{
    public int AdditionalAnswerID { get; set; }
    public AnswerTypeAnswerTypeEnum  AnswerType { get; set; }
}

如果我要创建新的Question,如何在AdditionalAnswers视图中添加多个Edit而不进行任何回复?

1 个答案:

答案 0 :(得分:1)

默认的模型绑定应该能够做到。诀窍是动态添加类似于:

的输入标签
<input name="AdditionalAnswer[0].AdditionalAnswerID"/>
<input name="AdditionalAnswer[0].AnswerType"/>
<input name="AdditionalAnswer[1].AdditionalAnswerID"/>
<input name="AdditionalAnswer[1].AnswerType"/>