简体   繁体   English

Contoso mvc3系统,多对多关系

[英]Contoso mvc3 system, many to many relationship

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 特别是从“将课程分配添加到教师编辑页面”标题开始

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 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 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: 有效地创建此屏幕的大量代码: 屏幕

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; CRUD系统的这种基本组件似乎可以处理一对多关系。 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. TLDR:使用MVC3 / Entity框架,是否有比上述更好的方法将对象与其他对象相关联。

Edit2: Here's an image of a quick Lightswitch application Edit2:这是一个快速的Lightswitch应用程序的图像 在此处输入图片说明 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. 如果我要实现此系统的MVC版本,则将上面所列代码的x3产生相同的效果。

Surely there is a more efficient solution. 当然,有一个更有效的解决方案。

It seems, based on your comments, that you are looking for MVC to be Lightswitch. 根据您的评论,您似乎正在寻找将MVC用作Lightswitch。 If that were the case, Microsoft would not have developed Lightswitch. 如果真是这样,微软将不会开发Lightswitch。

Microsoft offers many technologies, MVC, Web Pages (WebMatrix), WebForms, LightSwitch. Microsoft提供了许多技术,MVC,网页(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. 如果您正在使用MVC开发,则需要花费更多的精力来编写演示代码。 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. 如果您不想这样做,那么我建议选择其他技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM