插入多个实体到许多一对多连接表 [英] Inserting multiple entities into many-to-many linking table

查看:112
本文介绍了插入多个实体到许多一对多连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序的实体时间表实体之间的许多一对多的链接,链接他们被称为 StationTimetable ,其中还包含有关两个实体之间的联系的数据。当我创建一个新的时间表,我想在数据库中对应于创造新的(多) StationTimetable 项这个新的时间表

My web application has a many-to-many link between the Station and Timetable entities, and the entity that links them is called StationTimetable, which also holds data about the linkage between the two entities. When I create a new Timetable, I want to create new (and multiple) StationTimetable entries in the database to correspond to this new Timetable.

到目前为止,我已经成功地得到应用,当用户创建一个新的时间表,以节省只是一个 StationTimetable 项。我有点明白这是为什么,我已经尝试修改它来保存多个条目,但是我现在打在我的知识砖墙。

So far I've managed to get the application to save just one StationTimetable entry when the user creates a new timetable. I sort of understand why this is, and I've attempted to modify it to save multiple entries however I'm now hitting a brick wall in my knowledge.

所以,我有一个 TimetablesCreateViewModel 这是我的看法使用模型:

So, I have a TimetablesCreateViewModel which is the model my view uses:

public class TimetablesCreateViewModel
{
    public Timetable Timetable { get; set; }

    public IEnumerable<StationTimetable> StationTimetables { get; set; }

    public Station Station { get; set; }

    public SelectList StationList { get; set; }
}

在我看来,我让用户插入最多10 StationTimetable 条目:

In my view, I'm letting the user insert up to 10 StationTimetable entries:

@for (var i = 0; i < 10; i++)
{
    <tr>
        <td>@Html.DropDownListFor(model => model.StationTimetables.StationId, Model.StationList, "---", htmlAttributes: new { @class = "form-control col-md-2" })</td>
        <td>@Html.EditorFor(model => model.StationTimetables.Arrival, new { htmlAttributes = new { @class = "form-control" } })</td>
        <td>@Html.EditorFor(model => model.StationTimetables.Departure, new { htmlAttributes = new { @class = "form-control" } })</td>
    </tr>
}

,然后在我的控制器的方法,我想要做类似如下:

And then, in my controller method, I want to do something like the following:

public ActionResult Create(TimetablesCreateViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        db.Timetables.Add(viewModel.Timetable);

        // Somehow loop over viewModel.StationTimetables and add them here?
        // db.StationTimetables.Add(viewModel.StationTimetable);

        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(viewModel);
}

眼前的问题似乎是,在 DropDownListFor EditorFor 在我看来,方法都在抱怨,这使得因为我指定感 StationTimetables 在我的视图模型为的IEnumerable ,我无法访问的stationID 到达出发 的IEnumerable 。这些属性是实体的一部分的IEnumerable 成立。

The immediate problem seems to be that the DropDownListFor and EditorFor methods in my view are complaining, and that makes sense as I've specified StationTimetables in my view model to be IEnumerable, and I can't access the StationId, Arrival and Departure properties of IEnumerable. Those properties are part of the entity that IEnumerable holds.

不幸的是,我不知道如何使用 HTML 帮助创建一个表单,我可以填充 StationTimetables 与多家 StationTimetable 实体。如果我能得到多个 StationTimetable 项到我的创建的方法,我不认为我将有一个问题后,将它们添加到数据库中。

Unfortunately, I don't know how to use the Html helper to create a form where I can populate StationTimetables with a number of StationTimetable entities. If I could just get multiple StationTimetable entries to my Create method, I don't think I'll have a problem adding them to the database after that.

感谢您的任何帮助。

推荐答案

您需要让你的 StationTimetables 属性的IList 并使用索引,这样的属性正确命名,可以在回发的约束

You need to make your StationTimetables property IList and use the indexer so that the properties are correctly named and can be bound on post back

@for (var i = 0; i < 10; i++)
{
  <tr>
    <td>@Html.DropDownListFor(m => m.StationTimetables[i].StationId, Model.StationList, "---", htmlAttributes: new { @class = "form-control col-md-2" })</td>
    <td>@Html.EditorFor(m => m.StationTimetables[i].Arrival, new { htmlAttributes = new { @class = "form-control" } })</td>
    <td>@Html.EditorFor(m => m.StationTimetables[i].Departure, new { htmlAttributes = new { @class = "form-control" } })</td>
  </tr>
}

这将产生例如正确的名称属性。

This will generate the correct name attributes for example

<input name="StationTimetables[0].StationId" ...>
<input name="StationTimetables[1].StationId" ...>

如果你不能从的IEnumerable 改变,你可以创建一个自定义的 EditorTemplate 为typeof运算 StationTimetable

If you cant change it from IEnumerable, you can create a custom EditorTemplate for typeof StationTimetable

查看/共享/ EditorTemplates / StationTimetable.cshtml

Views/Shared/EditorTemplates/StationTimetable.cshtml

@model YourAssembly.StationTimetable
<tr>
  <td>@Html.DropDownListFor(m => m.StationId, (SelectList)ViewData["StationList"], "---", new { @class = "form-control col-md-2" })</td>
  <td>@Html.EditorFor(m => m.Arrival, new { htmlAttributes = new { @class = "form-control" } })</td>
  <td>@Html.EditorFor(m => m.Departure, new { htmlAttributes = new { @class = "form-control" } })</td>
</tr>

,然后在主视图

@EditorFor(m => m.StationTimetables, new { StationList = Model.Model.StationList })

这做法意味着你需要你通过模型视图前初始化与10个项目的 StationTimetables 属性在控制器中。

This approach means you need to initialize the StationTimetables property with 10 items in the controller before you pass the model to the view.

在任何情况下,我建议,因为在使用中的错误(报告,但尚未解决)在你的情况下, EditorFor()办法 DropDownListFor ()循环。

In any case, I recommend the EditorFor() approach in your case because of a bug (reported but not yet solved) in using DropDownListFor() in a for loop.

不过,我怀疑你真的不总是有整整10站时刻表(我让用户插入的最高 10 的),这使得这种方法不灵活 - 为例如,如果你添加验证属性的属性,除非所有的10次表属性被填入该模型将是无效的。这将是更好地使用JavaScript或的 BeginCollectionItem 帮手动态地添加一个新的时间表。这两种方法的一个例子是包含在<一href=\"http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308\">this回答

However I suspect you don't really always have exactly 10 station timetables (I'm letting the user insert up to 10) which makes this approach inflexible - for example if you add validation attributes to the properties, the model will be invalid unless all 10 time table properties are filled in. It would be better to use javascript or the BeginCollectionItem helper to dynamically add a new timetable. A example of both approaches is included in this answer

这篇关于插入多个实体到许多一对多连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆