2

嗨,我一直坚持这个问题。我看过很多例子,但我找不到我要找的东西。任何帮助表示赞赏。

我使用代码优先的方法。

我已启用迁移并且数据库很好 [学生] - [学生课程] - [课程]。

场景:我有两个实体 -> 学生

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<Course> Courses { get; set; }
}

和课程

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual List<Student> Students { get; set; }
}

没什么好看的...我创建了一个 ViewModel ->

public class StudentCourseViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Course> Courses { get; set; }
}

看法:

@model Project.Web.Models.StudentCourseViewModel @{ ViewBag.Title = "Edit"; }

编辑

@using (Html.BeginForm()) { @Html.ValidationSummary(true)

<fieldset>
    <legend>Student</legend>

    @Html.HiddenFor(model => model.Id)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Courses)
    </div>
      <div class="editor-field">
    @for (int i = 0; i < Model.Students.Count(); i++)
    {
        <div style="border: dotted 1px; padding: 5px; margin: 10px;">
            @Html.HiddenFor(s => s.Students[i].Id)

            @Html.LabelFor(s => s.Students[i].Name[i + 1])
            @Html.EditorFor(s => s.Students[i].Name)
        </div>
    }
</div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset> }

控制器动作:

    [HttpPost]
    public ActionResult Edit(CourseStudentViewModel model)
    {
        var course = db.Courses.Find(model.CourseId);
        course.Name = model.CourseName;
        course.Description = model.CourseDescription;
        course.Students = model.Students;

        if (ModelState.IsValid)
        {

            db.Entry(course).State = System.Data.EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        return View(model);
    }

(也许这是我出错了......)

无论如何,我想创建一个包含许多可选课程的新学生(文本框-> courseName)

我该怎么做?

主要问题是我总是从我的视图 [httpPost]Create -action 中得到空值(学生很好,课程列表 = NULL)。

我需要指导如何使这种方法成为可能。

谢谢!

4

1 回答 1

0

您的实体未正确设置多对多关系。您需要另一个实体来处理多对多映射。它看起来像这样。

public class StudentsToCourses
{
   public int StudentId {get; set;}
   public int CourseId {get; set;}

   public virtual Student Student {get; set;}
   public virtual Course Course {get; set;}
}

那么你的学生模型应该改为这个。

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<StudentsToCourses> Courses { get; set; }
}

你的课程模型变成了这个。

public class Course
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual List<StudentsToCourses> Students { get; set; }
}

您还需要使用Fluent API设置外键关系。它看起来像这样。

public class StudentsToCoursesConfiguration : EntityTypeConfiguration<StudentsToCourses>
{
    internal StudentsToCoursesConfiguration ()
    {
        this.HasKey(p => new {p.StudentId, p.CourseId});
        this.HasRequired(p => p.Student)
            .WithMany(p => p.Courses)
            .HasForeignKey(p => p.StudentId);
        this.HasRequired(p => p.Course)
            .WithMany(r => r.Students)
            .HasForeignKey(p => p.CourseId);

    }
}
于 2013-02-12T15:22:32.723 回答