How should i design my ViewModel and save it using AutoMapper and EF?
Im not quite sure how to use ViewModels so i need some help on the following issue:
I am creating a online survey. A user is able to create a multiple choice question. The domain entities that i use for this to happen are the following:
[Question]
int category_id { get; set; } // The category the question belongs to
int type_code { get; set; } // The type of question (I.E Multiple Choice)
string question_wording { get; set; } // The question itself
bool visible { get; set; } // Visiblity
int question_number { get; set; } // The number of the question
string help_text { get; set; } // Help text if the user doesnt understand the question
[Multiple_Choice_Question]
int choice_number { get; set; } // The order in which the MCQ answer possibility is shown
int choice_wording { get; set; } // The MCQ answer possibility
string help_text { get; set; } // help_text if the user doesnt understand the answer possibility
// This is a cross-reference table in my database that maps questions with choice possibilities
[Ref_Multiple_ChoiceAnswer]
int question_id { get; set; }
int mcq_id { get; set; }
In my View i need to be able to create the question and the choice possibilities (Multiple_Choice_Question) at the same time. The user writes the choice possibilties in a textbox, each seperated by a new line.
like
Cat
Dog
Mouse
Now that im working with two entities should i just put all the necessary properties in my ViewModel? Each Answer possibility is a new row in my database, and in the View it gets sent back as a string (the text in the textbox) - how do i solve thi开发者_运维百科s?
How do i use AutoMapper on [HttpPost] to bind the properties from the Question with a new Question Object and the answer possibilities with a Multiple_Choice_Question Object. Also, what is the best way to map theese two new entities in the Ref_Multiple_ChoiceAnswer table?
Thanks in advance
You are not supposed to bind props from view model. It`s so by design.
This drives design more towards oop.
State of Your model should be changed through invoked behavior.
E.g. instead of:
question.visible=questionViewModel.visible;
You should be using:
class QuestionController{
ActionResult Hide(int question){
var q=find(question);
q.Hide();
return q.As<QuestionViewModel>();
}
}
class Question{
void Hide(){
Visible=false;
}
}
That is why "binding properties" kind a makes no sense.
精彩评论