0

プロジェクトで最初に MVC3-Viewmodel モデルを使用しています。

ユーザーが my に値を入力DDLTextAreaてフォーム ボタンをクリックすると、基本的に 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同様にGradenull可能です。

エンティティは次のように関連付けられています

[Question](1)------(*)[SelectedQuestion](1)-----(0..1)[GoalCardQuestionAnswer]

どんな種類の助けも大歓迎です。

前もって感謝します!

4

1 に答える 1

0

私は自分の質問を達成し、答えは次のとおりです。

 [HttpPost]
        public JsonResult AnswerForm(int id, SelectedQuestionViewModel model)
        {
            SelectedQuestion SelectedQ = answerNKIRepository.GetSelectedQuestionByID(model.QuestionID);

            if (SelectedQ.GoalCardQuestionAnswer == null)
            {

                var goalCardQuestionAnswer = new GoalCardQuestionAnswer();
                goalCardQuestionAnswer.SelectedQuestion = SelectedQ;
                goalCardQuestionAnswer.SelectedQuestion.Id = model.QuestionID;
                goalCardQuestionAnswer.Comment = model.Comment;
                goalCardQuestionAnswer.Grade = model.Grade;
                this.answerNKIRepository.SaveQuestionAnswer(goalCardQuestionAnswer);
                this.answerNKIRepository.Save();
                const bool Result = true;
                return this.Json(Result);
            }
            else
            {
                if (SelectedQ.GoalCardQuestionAnswer != null)
                {
                    SelectedQ.GoalCardQuestionAnswer.Comment = model.Comment;
                }

                if (SelectedQ.GoalCardQuestionAnswer != null)
                {
                    SelectedQ.GoalCardQuestionAnswer.Grade = model.Grade;
                }
                const bool Result = false;
                return this.Json(Result);
            }
        }
于 2012-04-25T17:47:34.690 に答える