[英]Insert data to multiple tables in Create View ASP.Net MVC
I'm trying to insert data to two tables in a Create View
. 我试图将数据插入
Create View
两个表中。 What I have is a table User
and another table WorkingHours
. 我所拥有的是一个表
User
和另一个表WorkingHours
。 The WorkingHours
table defines the hours each User
works in a week. WorkingHours
表定义每个User
每周工作的小时数。 I'd like to be able to add the user's WorkingHours
at the same time you're adding a user, so it has to be the same View
. 我希望能够在添加用户的同时添加用户的
WorkingHours
,因此它必须是相同的View
。 I've been looking through examples how to do this, mainly working with PartialViews
. 我一直在浏览示例如何做到这一点,主要是与
PartialViews
一起使用。
I've followed this example to insert the data both to the Users
and WorkingHours
tables. 我已遵循此示例将数据插入到
Users
和WorkingHours
表中。
These are the classes of each table (I'm working Model First here): 这些是每个表的类(我在这里使用“ 模型优先” ):
User 用户
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; }
}
}
I've added two Partial Views for User and WorkingHours: 我为用户和WorkingHours添加了两个部分视图 :
User Partial View: 用户局部视图:
@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 WorkingHours部分视图
@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>
Partial Views Combined: 合并部分视图:
@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>
}
When creating the user, the object does have the user details...Name, Surname etc. but not the WorkingHours
, the list remains null
. 创建用户时,该对象确实具有用户详细信息...名称,姓氏等,但没有
WorkingHours
,列表保持为null
。 I thought adding ViewDataDictionary
would solve the issue but the list remains null
. 我以为添加
ViewDataDictionary
可以解决问题,但列表仍然为null
。 Not sure if this works differently with a list rather than an object as shown in the link above. 不知道这是否适用于列表而不是对象,如上面的链接所示。
UPDATE UPDATE
I might solve the issue by passing a User
object to the View
in the Create
method. 我可以通过将
User
对象传递给Create
方法中的View
来解决此问题。
public ActionResult Create()
{
User u = new Common.User();
return View(u);
}
And populate the WorkingHours
list in the User
constructor. 并在
User
构造函数中填充WorkingHours
列表。
将partial的模型更改为User
并渲染编辑器,如下所示:
@Html.EditorFor(model => model.WorkingHours.HoursFrom)
I've solved the issue by passing a User
object to the View
in the Create
method. 我已经通过在
Create
方法中将User
对象传递给View
解决了该问题。
public ActionResult Create()
{
User u = new Common.User();
return View(u);
}
I'm populating the WorkingHours
list in the User
constructor: 我在
User
构造函数中填充WorkingHours
列表:
// 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
});
}
In this case this makes sense as the list has default values for each day. 在这种情况下,这是有意义的,因为列表具有每天的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.