プロジェクトで最初に MVC3-Viewmodel モデルを使用しています。
ユーザーが my に値を入力DDL
しTextArea
てフォーム ボタンをクリックすると、基本的に POST アクションに対して ajax url.post が実行されます。現在、Post Action メソッドがそれを作成して保存します。しかし、私が欲しいのは、ある種のチェックです。例:
- ステップ 1: SelectQuestion に回答がある場合
- ステップ 2: 回答が存在する場合は、更新を行います
- ステップ 3: 回答が存在しない場合は、新規作成して保存します。
これは私のコントローラーが今どのように見えるかです:
[HttpPost]
public JsonResult AnswerForm(int id, SelectedQuestionViewModel model)
{
bool result = false;
var goalCardQuestionAnswer = new GoalCardQuestionAnswer(); // Creates an instance of the entity that I want to fill with data
SelectedQuestion SelectedQ = answerNKIRepository.GetSelectedQuestionByID(model.QuestionID); // Retrieve SelectedQuestion from my repository with my QuestionID.
goalCardQuestionAnswer.SelectedQuestion = SelectedQ; // Filling my entity with SelectedQ
goalCardQuestionAnswer.SelectedQuestion.Id = model.QuestionID; // filling my foreign key with the QuestionID
goalCardQuestionAnswer.Comment = model.Comment; // Filling my entity attribute with data
goalCardQuestionAnswer.Grade = model.Grade; // Filling my entity attribute with data
answerNKIRepository.SaveQuestionAnswer(goalCardQuestionAnswer); // adding my object
answerNKIRepository.Save(); // saving
result = true;
return Json(result);
}
Comment
同様にGrade
null可能です。
エンティティは次のように関連付けられています
[Question](1)------(*)[SelectedQuestion](1)-----(0..1)[GoalCardQuestionAnswer]
どんな種類の助けも大歓迎です。
前もって感謝します!