Rails has-many-through equivalent in ASP.NET MVC3

前端 未结 2 1475
借酒劲吻你
借酒劲吻你 2020-12-18 00:31

In .NET Entity Framework, what is the best way to have a (custom) join table with extra attributes (other than ids) and/or associate this join table with others via separate

2条回答
  •  一向
    一向 (楼主)
    2020-12-18 01:31

    UPDATE to Leniency's answer:

    We can also create two one-to-many relationships with Model first approach. Either way we cannot have model binding as happen in pure M2M relationship (without payload or pure join tables - PJT).

    Also, in (scaffold) controller, we can use a view model for the CRUD operations as per the requirement. Supposedly, we have a FloorViewModel with the following definition:

    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; }
    
    }
    

    The create action in controller would be:

    //
    // 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);
    }
    

    and the Edit action would be something like:

    //
    // 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(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);
    }
    

    In View, we can send the FloorModelView's object like:

    @model ManyToManyAutoGen.Models.FloorViewModel
    
    @{
        ViewBag.Title = "Create";
    }
    
    

    Create

    @using (Html.BeginForm()) { @Html.ValidationSummary(true)
    Floor @Html.Partial("_CreateOrEdit", Model)

    }
    @Html.ActionLink("Back to List", "Index")

    and finally, _CreateOrEdit partial looks like:

    @model ManyToManyAutoGen.Models.FloorViewModel
    
    @* This partial view defines form fields that will appear when creating and editing entities *@
    
    
    @Html.LabelFor(model => model.MyFloor.FloorName)
    @Html.EditorFor(model => model.MyFloor.FloorName) @Html.ValidationMessageFor(model => model.MyFloor.FloorName)
    @Html.LabelFor(model => model.MyStudents)
    @Html.ListBoxFor(model => model.MyStudents, Model.StudentsList) @Html.ValidationMessageFor(model => model.MyStudents)
    @Html.LabelFor(model => model.Join.First().Room)
    @Html.EditorFor(model => model.Join.First().Room) @Html.ValidationMessageFor(model => model.Join)

提交回复
热议问题