开发者

Asp.net with MVC multiple model in one view (create, update)

开发者 https://www.devze.com 2023-02-16 07:11 出处:网络
I have problem in asp.ne Mvc with multiple model in one view on create and update I \'m work on exams system

I have problem in asp.ne Mvc with multiple model in one view on create and update I 'm work on exams system class Questions and class Answers Question is aparent class and Answers is a child class

[Bind(exclude("id"))]

class Quesions
{
public string 开发者_StackOverflow中文版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;}
}

in a view I use two classes how to use classes in insert and update what a way which I have to solve my problem


You should wrap the two objects in another object for the View, commonly referred to as a "ViewModel".

public class QuestionAnswerViewModel
{
   public Question Question { get; set; }
   public ICollection<Answer> Answers { get; set; }
}

Bind to that in your View.

Then use something like AutoMapper to map it back to your two entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消