简体   繁体   中英

Insert data to multiple tables in Create View ASP.Net MVC

I'm trying to insert data to two tables in a Create View . What I have is a table User and another table WorkingHours . The WorkingHours table defines the hours each User works in a week. 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 . I've been looking through examples how to do this, mainly working with PartialViews .

I've followed this example to insert the data both to the Users and WorkingHours tables.

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:

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

@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 . I thought adding ViewDataDictionary would solve the issue but the list remains null . Not sure if this works differently with a list rather than an object as shown in the link above.

UPDATE

I might solve the issue by passing a User object to the View in the Create method.

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

And populate the WorkingHours list in the User constructor.

将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.

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

I'm populating the WorkingHours list in the User constructor:

// 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM