I am making an ASP.NET MVC application to function as a slideshow, with each slide having a question and an arbitrary list of multiple-choice answers, one of which is correct. I felt that I really had to force my solution to work, and I am sure that there are better ways to do it.Right now, the user edits the slide (edit and create are the same, so I just included edit) and uses a button to add answers. The button calls an AJAX function that calls an action in the controller. The controller creates a new Answer object, assigns it to the appropriate slide, saves it to the database, but then returns it with a blank slide to a View that creates the form for the answer.The created form for the answer is passed back to the AJAX function which appends it to the list of answers on the page. Correctness of an answer is determined by a set of radio buttons, one per answer. In order for the radio buttons to set the CorrectAnswerId property in the Slide model, the radio button must be created knowing the database Id of the answer, which is why the blank answer must be placed in the database.The blank answer must also be assigned to the slide, because otherwise there is no way to know for sure that the answer belongs with the slide, and therefore it would be replaced later, which would make the Id used by the radio button incorrect.To remove an answer, a button assigned to each answer calls a JavaScript function that simply deletes the HTML for the answer.Validation is not yet in place, so there is nothing to check if the user adds duplicate answers, doesn't select an answer, enters a question but no answers or vice versa, etc. This is just a very rough first demo of the basic functionality.Modelusing System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Data.Entity;using System.Web;namespace WebApplication2.Models{    public class Slide    {        public static int CurrentIndex = 0;        public virtual ICollection<Answer> Answers        {            get;            set;        } = new HashSet<Answer>();        public string Content        {            get;            set;        }        [Display(Name = Correct Answer)]        public int CorrectAnswerId        {            get;            set;        }        [Key]        [Column(Order = 0)]        public int Id        {            get;            set;        }        [Display(Name = Image)]        public byte[] ImageData        {            get;            set;        }        [Display(Name = Image Description)]        public string ImageDescription        {            get;            set;        }        public int Index        {            get;            set;        }        public string Question        {            get;            set;        }        public string Section        {            get;            set;        }        public string Title        {            get;            set;        }        [NotMapped]        public HttpPostedFileBase UploadedImage        {            get;            set;        }    }    public class Answer    {        public int Id        {            get;            set;        }        public virtual Slide Slide        {            get;            set;        }        public string Text        {            get;            set;        }    }    public class SlideDbContext : DbContext    {        public DbSet<Answer> Answers        {            get;            set;        }        public DbSet<Slide> Slides        {            get;            set;        }    }}'Edit' View@model WebApplication2.Models.Slide@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name=viewport content=width=device-width/>    <title>Edit</title>    <link rel=stylesheet href=../../CSS/Stylesheet.css/></head><body><script src=~/Scripts/jquery-3.1.1.min.js></script><script src=~/Scripts/jquery.validate.min.js></script><script src=~/Scripts/jquery.validate.unobtrusive.min.js></script><script>    jQuery(document).ready(function($) {        $(#add-answer).on('click',            function() {                jQuery.get('/Slide/AddAnswer', { slideId: @Model.Id }).done(function(html) {                    $(#answers-list).append(html);                });            });    });</script>@using (Html.BeginForm(null, null, FormMethod.Post, new{    enctype = multipart/form-data})){    @Html.AntiForgeryToken()    <div>        <h4>Slide</h4>        <hr/>        @Html.ValidationSummary(true, , new        {            @class = text-danger        })        @Html.EditorFor(x => x)        <h5>@Html.LabelFor(x => x.Answers)</h5>        <div id=answers-list>            @Html.EditorForMany(x => x.Answers)        </div>        <script>            $(input[name=CorrectAnswerId][value= + @Model.CorrectAnswerId + ]).prop(checked, true)        </script>        <input type=button id=add-answer value=Add New/>        <div class=form-group>            <div class=col-md-offset-2 col-md-10>                <input type=submit value=Save class=btn btn-default/>            </div>        </div>    </div>}<div>    @Html.ActionLink(Back to List, Index)</div><script>    function removeAnswer(answerId) {        $(#answer- + answerId).remove();    }</script></body></html>'Slide' View@model WebApplication2.Models.Slide<div class=form-group>    @Html.HiddenFor(x => x.Id)    <div>        <h5>@Html.LabelFor(x => x.Section)</h5>        @Html.TextBoxFor(x => x.Section)    </div>    <div>        <h5>@Html.LabelFor(x => x.Title)</h5>        @Html.TextBoxFor(x => x.Title)    </div>    <div>        <h5>@Html.LabelFor(x => x.Content)</h5>        @Html.TextAreaFor(x => x.Content)    </div>    <div>        <h5>@Html.LabelFor(x => x.ImageData)</h5>        <img src=@Url.Action(RenderImage, new                  {                      id = Model.Id                  }) alt=@Model.ImageDescription/>        <input type=file name=uploadedImage class=input-files/>    </div>    <div>        <h5>@Html.LabelFor(x => x.ImageDescription)</h5>        @Html.TextBoxFor(x => x.ImageDescription)    </div>    <div>        <h5>@Html.LabelFor(x => x.Question)</h5>        @Html.TextBoxFor(x => x.Question)    </div></div>'Answer' View@model WebApplication2.Models.Answer<div class=form-group id=answer-@Model.Id>    @Html.HiddenFor(x => x.Id)    @Html.TextBoxFor(x => x.Text)    <input type=radio name=CorrectAnswerId value=@Model.Id/>    <button onclick=removeAnswer(@Model.Id);return false;>Remove</button></div>'AddAnswer' View@using WebApplication2.Models@model WebApplication2.Models.Slide@{    Layout = null;}@Html.EditorForMany(x => x.Answers)Controller (Relevant Portions)private readonly SlideDbContext _database = new SlideDbContext();[OutputCache(NoStore = true, Duration = 0, VaryByParam = *)]public ActionResult AddAnswer(int slideId){    var blankSlide = new Slide();    var slide = _database.Slides.Find(slideId);    var answer = new Answer    {        Slide = slide    };    _database.Answers.Add(answer);    _database.SaveChanges();    blankSlide.Answers.Add(answer);    return View(blankSlide);}[ActionName(Edit)][HttpPost]public ActionResult Edit(Slide newSlide){    var oldSlide = _database.Slides.Find(newSlide.Id);    if (oldSlide == null)    {        return HttpNotFound();    }    oldSlide.Content = newSlide.Content;    oldSlide.ImageDescription = newSlide.ImageDescription;    oldSlide.Section = newSlide.Section;    oldSlide.Title = newSlide.Title;    oldSlide.Question = newSlide.Question;    oldSlide.CorrectAnswerId = newSlide.CorrectAnswerId;    if (newSlide.UploadedImage != null)    {        oldSlide.UploadedImage = newSlide.UploadedImage;        SaveImage(oldSlide);    }    foreach (var newAnswer in newSlide.Answers)    {        var originalAnswer = oldSlide.Answers            .SingleOrDefault(x => x.Id == newAnswer.Id && x.Id != 0);        if (originalAnswer != null)        {            var answerEntry = _database.Entry(originalAnswer);            answerEntry.CurrentValues.SetValues(newAnswer);        }        else        {            newAnswer.Id = 0;            oldSlide.Answers.Add(newAnswer);        }    }    foreach (var oldAnswer in oldSlide.Answers.Where(x => x.Id != 0)        .ToList())    {        if (newSlide.Answers.All(x => x.Id != oldAnswer.Id))        {            _database.Answers.Remove(oldAnswer);        }    }    _database.SaveChanges();    return RedirectToAction(Index);}
Slideshow with Multiple Choice Quiz - Adding/Editing/Removing Quiz Answers
c#;asp.net;entity framework;asp.net mvc