Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails在ASP.NET MVC3中具有许多等效功能_Ruby On Rails_Asp.net Mvc 3_Orm_Model_Many To Many - Fatal编程技术网

Ruby on rails Rails在ASP.NET MVC3中具有许多等效功能

Ruby on rails Rails在ASP.NET MVC3中具有许多等效功能,ruby-on-rails,asp.net-mvc-3,orm,model,many-to-many,Ruby On Rails,Asp.net Mvc 3,Orm,Model,Many To Many,在.NET实体框架中,使用(自定义)具有额外属性(ID除外)的联接表和/或通过单独的模型将此联接表与其他联接表关联的最佳方法是什么?在Ruby on Rails中,我们可以为联接表创建一个模型,如: Item.rb (model) :has_many => :buyers, :through=>:invoice ... Buyers.rb (model) :has_many => :items, :through=>:invoice ... Invoice.rb (m

在.NET实体框架中,使用(自定义)具有额外属性(ID除外)的联接表和/或通过单独的模型将此联接表与其他联接表关联的最佳方法是什么?在Ruby on Rails中,我们可以为联接表创建一个模型,如:

Item.rb (model)
:has_many => :buyers, :through=>:invoice
...

Buyers.rb (model)
:has_many => :items, :through=>:invoice
...

Invoice.rb (model)
:belongs_to :item
:belongs_to :buyer
....
然后我们可以使用:
Item.first.bullers
bullers.first.items
buller.create(:items=>Item.create(:name=>'random'))
等。就像我们使用没有模型的自动联接表一样(使用has\u和\u-beliens\u-to\u-many)


在VisualStudio2010的“添加关联”对话框中,如果将多重性选择为*(多个),则没有选择联接表(使用模型)的选项。有没有一种手动操作的方法?

是的,您可以获得非常接近的结果。我不太确定如何在设计器中设置它,因为我只使用codefirst

下面是一个例子:


学生->学生楼层更新宽恕的答案:

我们还可以使用模型优先方法创建两个一对多关系。无论哪种方式,我们都不能像纯M2M关系中那样进行模型绑定(没有有效负载或纯联接表-PJT)

此外,在(scaffold)controller中,我们可以根据需要为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);
}
编辑操作将类似于:

//
// 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);
}
//
//获取:/Floor/编辑
公共操作结果编辑(int id)
{
Floor=context.Floors.Single(x=>x.Id==Id);
int[]Id=(从floor.StudentFloorJoins中的i选择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};
返回视图(模型);
}
//
//张贴/发言/编辑
[HttpPost]
公共行动结果编辑(FloorViewModel floor)
{
if(ModelState.IsValid)
{
var item=floor.MyFloor;
var itemEntry1=context.Entry(项目);
itemEntry1.State=EntityState.Modified;
var query=(来自context.StudentFloorJoins中的i,其中i.Floor.Id==item.Id选择i.Id);
foreach(查询中的var studentfloor)
{
context.StudentFloorJoins.Remove(context.StudentFloorJoins.Find(studentfloor));
}
SaveChanges();
foreach(地板上的var学生。我的学生)
{
context.StudentFloorJoins.Add(新StudentFloorJoin(){Student=context.Students.Find(Student),Floor=Floor.MyFloor,Room=Floor.Join.Room});
}
SaveChanges();
返回操作(“索引”);
}
floor.StudentsList=新的多选列表(context.Students,“Id”,“Name”,floor.MyStudents);
返回视图(楼层);
}
在视图中,我们可以发送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>
@model ManyToManyAutoGen.Models.FloorViewModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
地板
@Html.Partial(“_CreateOrEdit”,Model)

} @ActionLink(“返回列表”、“索引”)
最后,_createOreditpartial看起来像:

@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>
@model ManyToManyAutoGen.Models.FloorViewModel
@*此局部视图定义创建和编辑实体时将显示的表单字段*@
@LabelFor(model=>model.MyFloor.FloorName)
@EditorFor(model=>model.MyFloor.FloorName)
@Html.ValidationMessageFor(model=>model.MyFloor.FloorName)
@LabelFor(model=>model.MyStudents)
@Html.ListBoxFor(model=>model.MyStudents,model.StudentsList)
@Html.ValidationMessageFor(model=>model.MyStudents)
@LabelFor(model=>model.Join.First().Room)
@EditorFor(model=>model.Join.First().Room)
@Html.ValidationMessageFor(model=>model.Join)

感谢您的输入。请检查更新部分,让我知道控制器中的代码是否可以变得更薄、更光滑。我正在寻找您提到的控制器更新部分。。。找不到?那太糟糕了。看起来它已经被其他用户拒绝了,尽管它只是你答案的续集。让我把它放在一个单独的答案中。有人能继续控制器索引操作中的代码吗?@Branislav,检查下面我的答案。你会明白的。
//
// 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);
}
@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>
@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>