Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 3 - multiple checkbox list
    primarykey
    data
    text
    <p>I read tutorial "Creating an Entity Framework Data Model for an ASP.NET MVC Application" from <a href="http://www.asp.net/" rel="nofollow">http://www.asp.net/</a> and wanted to use multiple checkbox list from part 6 - <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application</a></p> <p>In this tutorial, it is only possible to access these checkbox options (to assign courses to each instrucor) from edit page of instructor. I want to render these checkboxes on the create page, but couldn't adjust codes I've found in this tutorial.</p> <p>These are parts of the codes that are used to display and use checkboxes for new inputs to database.</p> <p>HttpGet Edit method</p> <pre><code>public ActionResult Edit(int id) { Instructor instructor = db.Instructors .Include(i =&gt; i.OfficeAssignment) .Include(i =&gt; i.Courses) .Where(i =&gt; i.InstructorID == id) .Single(); PopulateAssignedCourseData(instructor); return View(instructor); } private void PopulateAssignedCourseData(Instructor instructor) { var allCourses = db.Courses; var instructorCourses = new HashSet&lt;int&gt;(instructor.Courses.Select(c =&gt; c.CourseID)); var viewModel = new List&lt;AssignedCourseData&gt;(); foreach (var course in allCourses) { viewModel.Add(new AssignedCourseData { CourseID = course.CourseID, Title = course.Title, Assigned = instructorCourses.Contains(course.CourseID) }); } ViewBag.Courses = viewModel; } </code></pre> <p>HttpPost Edit method</p> <pre><code>[HttpPost] public ActionResult Edit(int id, FormCollection formCollection, string[] selectedCourses) { var instructorToUpdate = db.Instructors .Include(i =&gt; i.OfficeAssignment) .Include(i =&gt; i.Courses) .Where(i =&gt; i.InstructorID == id) .Single(); if (TryUpdateModel(instructorToUpdate, "", null, new string[] { "Courses" })) { try { if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location)) { instructorToUpdate.OfficeAssignment = null; } UpdateInstructorCourses(selectedCourses, instructorToUpdate); db.Entry(instructorToUpdate).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } catch (DataException) { //Log the error (add a variable name after DataException) ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } } PopulateAssignedCourseData(instructorToUpdate); return View(instructorToUpdate); } private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate) { if (selectedCourses == null) { instructorToUpdate.Courses = new List&lt;Course&gt;(); return; } var selectedCoursesHS = new HashSet&lt;string&gt;(selectedCourses); var instructorCourses = new HashSet&lt;int&gt; (instructorToUpdate.Courses.Select(c =&gt; c.CourseID)); foreach (var course in db.Courses) { if (selectedCoursesHS.Contains(course.CourseID.ToString())) { if (!instructorCourses.Contains(course.CourseID)) { instructorToUpdate.Courses.Add(course); } } else { if (instructorCourses.Contains(course.CourseID)) { instructorToUpdate.Courses.Remove(course); } } } } </code></pre> <p>And the view</p> <pre><code>&lt;div class="editor-field"&gt; &lt;table&gt; &lt;tr&gt; @{ int cnt = 0; List&lt;ContosoUniversity.ViewModels.AssignedCourseData&gt; courses = ViewBag.Courses; foreach (var course in courses) { if (cnt++ % 3 == 0) { @: &lt;/tr&gt; &lt;tr&gt; } @: &lt;td&gt; &lt;input type="checkbox" name="selectedCourses" value="@course.CourseID" @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) /&gt; @course.CourseID @: @course.Title @:&lt;/td&gt; } @: &lt;/tr&gt; } &lt;/table&gt; &lt;/div&gt; </code></pre> <p>So, I want to insert multiple choices (select choices) to database, but would like to display this list of checkboxes immediately on the create page, not the edit page like it was done in the tutorial. If you have another solution that is not related to this tutorial, I would also be grateful. I know that I can't use these methods because they're methods for Edit, so they're takin "id" as parameter, but I hope that I can adjust them to be used in create page of my entity.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload