I have a list of 5 string values (possible answers for each question) which I want from user to type in. But when I submit form an error shows up: "Collection is read-only". What is wrong here?

Here is my view model:

public class QuestionVM
    {
        public QuestionVM() {
            PossibleAnswers = new string[5];
        }
        public QuestionVM(Question question) : this()
        {
            ID = question.ID;
            Text = question.Text;
            IsAssociatedWithProfessor = question.IsAssociatedWithProfessor;
            IsAssociatedWithAssistant = question.IsAssociatedWithAssistant;
        }
        public int? ID { get; set; }
        public string Text { get; set; }
        public bool IsAssociatedWithProfessor { get; set; }
        public bool IsAssociatedWithAssistant { get; set; }
        public IEnumerable<string> PossibleAnswers { get; set; }
        public Question ToQuestion(ApplicationDbContext context)
        {
            Question question = new Question
            {
                Text = this.Text,
                IsAssociatedWithProfessor = this.IsAssociatedWithProfessor,
                IsAssociatedWithAssistant = this.IsAssociatedWithAssistant
            };
            //ID will be null if creating new question
            if(ID != null)
            {
                question.ID = (int) ID;
            }
            foreach (string possibleAnswer in this.PossibleAnswersVM.PossibleAnswers)
            {
                if (!String.IsNullOrEmpty(possibleAnswer))
                {
                    PossibleAnswer existingPossibleAnswer = context.PossibleAnswers.SingleOrDefault(ans => ans.Text == possibleAnswer);
                    if (existingPossibleAnswer == null)
                    {
                        PossibleAnswer ans = new PossibleAnswer { Text = possibleAnswer };
                        context.PossibleAnswers.Add(ans);
                        question.PossibleAnswers.Add(ans);
                    }
                    else
                    {
                        question.PossibleAnswers.Add(existingPossibleAnswer);
                    }
                }
            }
            return question;
        }
    }

My post method in controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddQuestion(QuestionVM questionVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Question question = questionVM.ToQuestion(context);
                    context.Questions.Add(question);
                    context.SaveChanges();
                    return RedirectToAction("Questions");
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Trenutno nije moguće snimiti promjene, pokušajte ponovo.");
            }
            return View(questionVM);
        }

Part of view:

@Html.EditorFor(model => model.PossibleAnswers, "PossibleAnswers")

And template:

@model IEnumerable<string>
@{
    ViewBag.Title = "PossibleAnswers";
}

@foreach (var str in Model)
{
       @Html.TextBoxFor(m => str)
}

Related posts

Recent Viewed