0

I am using MVC3-Viewmodel model first on my project.

When a user enters a value in my DDL and TextArea and then click on my form button it will basicly execute a ajax url.post to my POST action, right now my Post Action method creates and saves it. But what I want is some type of check, example:

  • step 1: If SelectQuestion has any answer
  • step 2: If answer exist do an update
  • step 3: if answer do not exist create a new and save it.

This is how my controller looks like now:

   [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 and Grade are nullable aswell.

The entitys are associated like

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

Any kind of help is appreciated.

Thanks in advance!

Obsivus
  • 8,231
  • 13
  • 52
  • 97

1 Answers1

0

I achieved my question and the answer is following:

 [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);
            }
        }
Obsivus
  • 8,231
  • 13
  • 52
  • 97