Asp.net在一个视图中使用MVC多个模型(创建,更新)

Abd*_*men 2 asp.net-mvc

我在asp.ne Mvc中有问题在一个视图中有多个模型创建和更新我在考试系统类上工作问题和类答案问题是aparent类和Answers是一个子类

[Bind(exclude("id"))]

class Quesions
{
public string question{get; set;}
public Datetime Timepostquestion{get; set;}
}
[Bind(exclude("id"))]
class Answers
{
public string answer{get; set;}
public Datetime Timepostanswer{get; set;}
public questionId {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

在视图中我使用两个类如何在插入中使用类并更新我必须解决问题的方法

RPM*_*984 7

您应该将两个对象包装在View的另一个对象中,通常称为"ViewModel".

public class QuestionAnswerViewModel
{
   public Question Question { get; set; }
   public ICollection<Answer> Answers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在你的视图中绑定到那个.

然后使用类似AutoMapper的东西将它映射回你的两个实体.