Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:21:53+00:00 2026-06-01T21:21:53+00:00

I have been following the Microsoft Contoso MVC3 University Tutorial located here I have

  • 0

I have been following the Microsoft Contoso MVC3 University Tutorial located here

I have a question regarding one area, In part 6; the tutorial explains how to create a table of courses an instructor can be assigned to using checkboxes. Specifically from the heading “adding Course Assignments to the Instructor Edit Page” onward

It seems overly complex, is there a more efficient way of doing things? Plug-in/built-in system etc.
What if you wanted to expand the system so an instructor had more than just Courses assigned to him? The duplication of code would be huge.

InstructorController

    // GET: /Instructor/Edit/5
    public ActionResult Edit(int id)
    {
        Instructor instructor = db.Instructors
            .Include(i => i.OfficeAssignment)
            .Include(i => i.Courses)
            .Where(i => i.InstructorID == id)
            .Single();
        PopulateAssignedCourseData(instructor);
        return View(instructor);

    }

    private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate)
    {
        if (selectedCourses == null)
        {
            instructorToUpdate.Courses = new List<Course>();
            return;
        }

        var selectedCoursesHS = new HashSet<string>(selectedCourses);
        var instructorCourses = new HashSet<int>
            (instructorToUpdate.Courses.Select(c => 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);
                }
            }
        }
    }

    private void PopulateAssignedCourseData(Instructor instructor)
    {
        var allCourses = db.Courses;
        var instructorCourses = new HashSet<int>(instructor.Courses.Select(c => c.CourseID));
        var viewModel = new List<AssignedCourseData>();
        foreach (var course in allCourses)
        {
            viewModel.Add(new AssignedCourseData
            {
                CourseID = course.CourseID,
                Title = course.Title,
                Assigned = instructorCourses.Contains(course.CourseID)
            });
        }
        ViewBag.Courses = viewModel;
    }

edit.cshtml

    int cnt = 0;
    List<UniversitySystem.ViewModels.AssignedCourseData> courses = ViewBag.Courses;

    foreach (var course in courses)
    {
        if (cnt++ % 3 == 0)
        {
                        @:  </tr> <tr> 
                    }
                    @: <td> 
                        <input type="checkbox" 
                               name="selectedCourses" 
                               value="@course.CourseID" 
                               @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) /> 
                        @course.CourseID @:  @course.Title
                    @:</td>
                }
                @: </tr>
            }
    </table>
</div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

ViewModels/AssignedCourseData.cs

    public class AssignedCourseData
    {
        public int CourseID { get; set; }
        public string Title { get; set; }
        public bool Assigned { get; set; }
    }
}

A great deal of code to effectivly create this screen:
Screen

I guess you could generalise the helper methods used in the InstructorController, but that is no small task.

It seems such a fundamental component of CRUD systems to deal with one/many to many relationships; I am surprised I cannot find information on the topic.

TLDR: Is there a better way to associate objects to other objects using MVC3/Entity framework than what is shown above.

Edit2:
Here’s an image of a quick Lightswitch application
enter image description here
A Candidate can have a number of Skills, Disabilities and Offences related to them. If i were to implement a MVC version of this system i would x3 of the code listed above to create the same effect.

Surely there is a more efficient solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:21:55+00:00Added an answer on June 1, 2026 at 9:21 pm

    It seems, based on your comments, that you are looking for MVC to be Lightswitch. If that were the case, Microsoft would not have developed Lightswitch.

    Microsoft offers many technologies, MVC, Web Pages (WebMatrix), WebForms, LightSwitch. Each has it’s own unique strengths and weaknesses, and you choose the technology that fits your requirements the best.

    If you’re developing in MVC, you need to expend more effort in writing presentation code. But, this extra effort gives you excellent flexibility in how that presentation works, what it looks like, and how it behaves. If you don’t want to do that, then I suggest choosing a different technology.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been following the affableBean tutorial from the NetBeans site located here .
I have been following the tutorial on Microsofts website and they use GameTime to
I have been following the answer of this question: How to update existing object
I have been following a couple of articles regarding RESTful web services with WCF
I have been following the validation for Entry boxes from here . The code
I have been following a tutorial on how to write a basic tile map
I have been following a short tutorial to build a tab menu on my
I have been following the MSDN tutorial as a reference while making an MVC
I have been referring to the following page: http://msdn.microsoft.com/en-us/library/ms178129.aspx I simply want to bulk
I have been following the example on the bottom of this page: http://msdn.microsoft.com/en-us/library/microsoft.windows.controls.ribbon.ribbonapplicationmenu.auxiliarypanecontent.aspx to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.