Let's imagine I have the following situation:
I have a question form and I want to add answer choices to it (my user should be allowed to add as many choices as he wants).
So I have this ViewModel (being sent to the view).
public class QuestionEdit
{
public int Id { get; set; }
[Required]
[StringLength(200)]
public string Question { get; set; }
public List<Choice> Choices { get; set; }
}
public class Choice
{
public int Id { get; set; }
[Required]
[StringLength(200)]
public string Choice { get; set; }
public bool Correct {get; set;^}
}
My Controller's Edit Post looks like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Question,Choices")] QuestionEdit vm)
{
if (ModelState.IsValid)
{
/*Removed for clarity*/
}
return View(vm)
}
Is there any way of me getting the content from Choices
and converting it to a javascript list, changing it as my user adds or removes items only by using javascript/ajax and when submitting the form, would it be a way for me to adding it back as if it were the Choices
that my controller will be able to read?
I am aware that I could use the an custom EditorFor for this List, but everytime someone adds a new Choice I would have to post the entire form to add it and them get it back, so I wanted to change this only through javascript, as it requires only simple validations (not needing any server/database validation).
Thanks a lot.