2011-08-01 36 views
0

我不太清楚如何使用ViewModels,所以我需要一些關於以下問題的幫助:我應該如何設計我的ViewModel並使用AutoMapper和EF保存它?

我正在創建一個在線調查。 用戶可以創建多選題。我使用的這種情況發生的領域實體如下:

[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; } 

在我看來,我需要能夠創建問題和選擇的可能性(Multiple_Choice_Question)在同一時間。用戶將選擇可能性寫入一個文本框中,每一行都由一個新行分隔。

Cat 
Dog 
Mouse 

現在,即時通訊有兩個實體的工作,我應該只是把我的ViewModel所有必要的屬性?每個答案的可能性是在我的數據庫中的一個新行,並在視圖中發送回作爲一個字符串(文本框中的文本) - 我該如何解決這個問題?

如何在[HttpPost]上使用AutoMapper將問題的屬性與新的問題對象綁定,並使用Multiple_Choice_Question對象回答可能性。此外,在Ref_Multiple_ChoiceAnswer表中映射這兩個新實體的最佳方法是什麼?

在此先感謝

回答

0

您不應該從視圖模型綁定道具。它是如此的設計。

這使得設計更多地朝向oop。
您的模型的狀態應通過調用的行爲進行更改。

E.g.而不是:

question.visible=questionViewModel.visible; 

您應該使用:

class QuestionController{ 
ActionResult Hide(int question){ 
    var q=find(question); 
    q.Hide(); 
    return q.As<QuestionViewModel>(); 

} 
} 
class Question{ 
    void Hide(){ 
    Visible=false; 
    } 
} 

那就是爲什麼 「綁定屬性」 實物是沒有意義的。

+0

嘿謝謝你的答案。這不是我正在尋找的答案,但是因爲你是唯一回答的人,所以我會將其標記爲答案。 – Nanek

+0

@Nanek http://bit.ly/p8shJh由Jimmy轉發 - Automapper本人作者:) –

相關問題