Contoso mvc3系统,关系多对多

时间:2022-02-13 02:16:14

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

我一直在关注位于此处的Microsoft Contoso MVC3大学教程

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

我对一个领域有疑问,在第6部分;本教程解释了如何创建课程表,可以使用复选框指定教师。特别是从标题“向教师编辑页面添加课程作业”开始

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: Contoso mvc3系统,关系多对多

大量的代码有效地创建了这个屏幕:

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

我想你可以概括一下InstructorController中使用的辅助方法,但这不是一件小事。

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.

CRUD系统似乎是处理一对多对多关系的基本组成部分;我很惊讶我无法找到有关该主题的信息。

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

TLDR:使用MVC3 / Entity框架将对象与其他对象关联的方法比上面显示的更好。

Edit2: Here's an image of a quick Lightswitch application Contoso mvc3系统,关系多对多 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.

编辑2:这是一个快速Lightswitch应用程序的图像候选人可以拥有许多与他们相关的技能,残疾和攻击。如果我要实现此系统的MVC版本,我将使用上面列出的x3代码来创建相同的效果。

Surely there is a more efficient solution.

当然有一个更有效的解决方案。

1 个解决方案

#1


1  

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.

根据您的评论,您似乎正在寻找MVC作为Lightswitch。如果是这样的话,微软就不会开发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.

Microsoft提供了许多技术,MVC,Web页面(WebMatrix),WebForms,LightSwitch。每个人都有自己独特的优势和劣势,您可以选择最符合您要求的技术。

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.

如果您正在使用MVC进行开发,则需要花费更多精力编写表示代码。但是,这种额外的努力使您在演示文稿的工作方式,外观和行为方面具有出色的灵活性。如果您不想这样做,那么我建议您选择其他技术。

#1


1  

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.

根据您的评论,您似乎正在寻找MVC作为Lightswitch。如果是这样的话,微软就不会开发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.

Microsoft提供了许多技术,MVC,Web页面(WebMatrix),WebForms,LightSwitch。每个人都有自己独特的优势和劣势,您可以选择最符合您要求的技术。

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.

如果您正在使用MVC进行开发,则需要花费更多精力编写表示代码。但是,这种额外的努力使您在演示文稿的工作方式,外观和行为方面具有出色的灵活性。如果您不想这样做,那么我建议您选择其他技术。