在Create View ASP.Net MVC中将数据插入多个表

时间:2014-12-22 11:05:39

标签: c# entity-framework asp.net-mvc-4 partial-views

我正在尝试将数据插入Create View中的两个表。我所拥有的是一个表User和另一个表WorkingHoursWorkingHours表定义每个User在一周内工作的小时数。我希望能够在您添加用户的同时添加用户的WorkingHours,因此它必须与View相同。我一直在查看如何执行此操作的示例,主要是使用PartialViews

我已按照此example将数据插入UsersWorkingHours表。

这些是每个表的类(我在这里工作模型第一):

用户

namespace Common
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public User()
        {
            this.WorkingHours = new HashSet<WorkingHours>();
        }

        public int ID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }

        public virtual ICollection<WorkingDay> WorkingDays { get; set; }
    }
}

WorkingHours

namespace Common
{
    using System;
    using System.Collections.Generic;

    public partial class WorkingHours
    {
        public int ID { get; set; }
        public Nullable<int> WeekDay { get; set; }
        public Nullable<int> HoursFrom { get; set; }
        public Nullable<int> HoursTo { get; set; }
        public Nullable<int> UserId { get; set; }

        public virtual User User { get; set; }
    }
}

我为User和WorkingHours添加了两个部分视图

用户偏见:

    @model Common.User

    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

<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.Surname)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Surname)
    @Html.ValidationMessageFor(model => model.Surname)
</div>

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

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

WorkingHours Partial View

@model Common.WorkingHours

<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

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

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

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

部分视图合并:

@model Common.User

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm("Create", "User", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    @Html.Partial("_UserCreate")
    @Html.Partial("_WorkingDayCreate", new ViewDataDictionary()
        {
            TemplateInfo = new TemplateInfo()
            {
                HtmlFieldPrefix = "WorkingDay"
            }
        }
    )

    <p>
        <input type="submit" value="Create" />
    </p>
}

创建用户时,对象确实拥有用户详细信息...名称,姓氏等但不包括WorkingHours,列表仍为null。我认为添加ViewDataDictionary可以解决问题,但列表仍为null。不确定这是否与列表而不是上面的链接中显示的对象的工作方式不同。

更新

我可以通过将User对象传递到View方法中的Create来解决此问题。

public ActionResult Create()
{
    User u = new Common.User();
    return View(u);
}

并填充WorkingHours构造函数中的User列表。

2 个答案:

答案 0 :(得分:0)

将部分模型更改为User并渲染编辑器,如下所示:

@Html.EditorFor(model => model.WorkingHours.HoursFrom)

答案 1 :(得分:0)

我通过将User对象传递给View方法中的Create来解决了这个问题。

public ActionResult Create()
{
    User u = new Common.User();
    return View(u);
}

我填充WorkingHours构造函数中的User列表:

// Monday to Friday (0 to 4 - using an enum: DaysEnum), default hours 08:00 to 17:00
for (int i = 0; i < 5; i++)
{
    this.WorkingHours.Add(new WorkingHour
    {
        WeekDay = i,
        HoursFrom = 8,
        HoursTo = 17
    });
}

在这种情况下,这是有道理的,因为列表具有每天的默认值。