ОБНОВЛЕНИЕ к ответу покаяния:
Мы также можем создать два отношения «один ко многим» с помощью подхода «Модель сначала». В любом случае у нас не может быть привязки модели, как это происходит в чистых отношениях M2M (без полезной нагрузки или таблиц чистого соединения - PJT).
Кроме того, в (scaffold) контроллере мы можем использовать модель представления для операций CRUD согласно требованию. Предположительно, у нас есть FloorViewModel со следующим определением:
public class FloorViewModel
{
private Model2Container context = new Model2Container();
[Display(Name = "Student List")]
[Required(ErrorMessage = "Please select atleast one student from the list.")]
public int[] MyStudents { get; set; }
public Floor MyFloor { get; set; }
public MultiSelectList StudentsList { get; set; }
public StudentFloorJoin Join { get; set; }
}
Действие создания в контроллере будет:
//
// GET: /Floor/Create
public ActionResult Create()
{
var model = new FloorViewModel() { StudentsList = new MultiSelectList(context.Students, "Id", "Name") };
return View(model);
}
//
// POST: /Floor/Create
[HttpPost]
public ActionResult Create(FloorViewModel floor)
{
if (ModelState.IsValid)
{
context.Floors.Add(floor.MyFloor);
context.SaveChanges();
}
foreach (var student in floor.MyStudents)
{
context.StudentFloorJoins.Add(new StudentFloorJoin() { Student = context.Students.Find(student), Floor = floor.MyFloor, Room = floor.Join.Room });
}
if (ModelState.IsValid)
{
context.SaveChanges();
return RedirectToAction("Index");
}
context.Floors.Remove(floor.MyFloor);
floor.StudentsList = new MultiSelectList(context.Students, "Id", "Name", floor.MyStudents);
return View(floor);
}
и действие Edit будет выглядеть примерно так:
//
// GET: /Floor/Edit
public ActionResult Edit(int id)
{
Floor floor = context.Floors.Single(x => x.Id == id);
int[] ids = (from i in floor.StudentFloorJoins select i.Student.Id).ToArray();
var model = new FloorViewModel() { StudentsList = new MultiSelectList(context.Students, "Id", "Name", ids), MyFloor = floor, Join = new StudentFloorJoin() { Room = floor.StudentFloorJoins.Count == 0 ? "" : floor.StudentFloorJoins.First().Room } };
return View(model);
}
//
// POST: /Floor/Edit
[HttpPost]
public ActionResult Edit(FloorViewModel floor)
{
if (ModelState.IsValid)
{
var item = floor.MyFloor;
var itemEntry1 = context.Entry<Floor>(item);
itemEntry1.State = EntityState.Modified;
var query = (from i in context.StudentFloorJoins where i.Floor.Id == item.Id select i.Id);
foreach (var studentfloor in query)
{
context.StudentFloorJoins.Remove(context.StudentFloorJoins.Find(studentfloor));
}
context.SaveChanges();
foreach (var student in floor.MyStudents)
{
context.StudentFloorJoins.Add(new StudentFloorJoin() { Student = context.Students.Find(student), Floor = floor.MyFloor, Room = floor.Join.Room });
}
context.SaveChanges();
return RedirectToAction("Index");
}
floor.StudentsList = new MultiSelectList(context.Students, "Id", "Name", floor.MyStudents);
return View(floor);
}
В View мы можем отправить объект FloorModelView, например:
@model ManyToManyAutoGen.Models.FloorViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Floor</legend>
@Html.Partial("_CreateOrEdit", Model)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
и, наконец, _CreateOrEdit частично выглядит так:
@model ManyToManyAutoGen.Models.FloorViewModel
@* This partial view defines form fields that will appear when creating and editing entities *@
<div class="editor-label">
@Html.LabelFor(model => model.MyFloor.FloorName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MyFloor.FloorName)
@Html.ValidationMessageFor(model => model.MyFloor.FloorName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MyStudents)
</div>
<div class="editor-field">
@Html.ListBoxFor(model => model.MyStudents, Model.StudentsList)
@Html.ValidationMessageFor(model => model.MyStudents)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Join.First().Room)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Join.First().Room)
@Html.ValidationMessageFor(model => model.Join)
</div>