无法使用ASP.NET EF6将数据保存到多个表中

时间:2018-03-20 08:27:25

标签: c# asp.net entity-framework-6

在我网站的某个页面上,某人可以更改有关设备的信息。

此数据来自2个不同的表格。将数据保存到DeviceStatus表中是没有问题的。

但出于某种原因,我无法将Active字段保存到concremodeDevice表中。保存此表中的所有其他数据没有问题。

代码:

控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,DeviceConfig_id,Device_statustypes_id,ConcremoteDevice_id,Employee_1,Employee_2,Sign_Date,Active")] DeviceStatus deviceStatus, ConcremoteDevice concremoteDevice)
{
    var Conn = (from d in db.DeviceStatus
                join s in db.Device_statustypes on d.Device_statustypes_id equals s.id
                join b in db.ConcremoteDevice on d.ConcremoteDevice_id equals b.id
                join c in db.DeviceConfig on d.DeviceConfig_id equals c.Device_config_id
                select new { s.id, Model = d.id });

    if (ModelState.IsValid)
    {
        db.Entry(deviceStatus).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    //    ViewBag.device_type_id = new SelectList(db.DeviceType, "device_type_id", "device_type", concremoteDevice.id);
    return View(deviceStatus);
}

页:

@model ConcremoteDeviceManagment.Models.DeviceStatus

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ConcremoteDevice</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.id)
        @Html.HiddenFor(model => model.DeviceConfig_id)
        @Html.HiddenFor(model => model.Device_statustypes_id)

        <div class="form-group">
            @Html.Label("Serie nummer", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConcremoteDevice_id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConcremoteDevice_id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Device Type", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DisplayFor(model => model.DeviceConfig.DeviceType.name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DeviceConfig.DeviceType.name, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Config ID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DeviceConfig.Device_config_id, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Medewerker 1", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Employee_1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Employee_1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Medewerker 2", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Employee_2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Employee_2, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Signeer datum", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Sign_Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Sign_Date, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Huidige status", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("StatusList", null, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Device_Statustypes.name, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("In Gebruik", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="col-md-10 checkbox">
                    @Html.EditorFor(model => model.ConcremoteDevice.Active)
                    @Html.ValidationMessageFor(model => model.ConcremoteDevice.Active, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

表:

CREATE TABLE [dbo].[ConcremoteDevice] (
    [id]     NVARCHAR (50) NOT NULL,
    [Active] BIT           NULL,
    CONSTRAINT [PK_ConcremoteDevice] PRIMARY KEY CLUSTERED ([id] ASC) WITH (FILLFACTOR = 65)
);

所以我的问题是,如果有人知道我为什么不能保存Active

正如pinkfloydx33在评论中所建议的,我在某些时候也试图设置concremodeDevice的状态,但这给了我下一个错误:

The key field 'id' cannot have a value of null. A non-null value is required for the key fields defined on type 'ConcremoteDevice'
BTW,不知道EF6的ASP.NET是否与此有关,但仅仅是为了确定它。

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

控制器:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "id,DeviceConfig_id,Device_statustypes_id,ConcremoteDevice_id,Employee_1,Employee_2,Device_statustypes_id,Sign_Date,Active")] DeviceStatus deviceStatus, ConcremoteDevice concremoteDevice)
        {

            if (ModelState.IsValid)
            {
                db.Entry(deviceStatus).State = EntityState.Modified;
                db.Entry(concremoteDevice).State = EntityState.Modified;
                db.SaveChanges();
                TempData["AlertMessage"] = "Device Edited Successfully";
                return RedirectToAction("Index");
            }
            return View(deviceStatus);
        }

并查看:

<div class="form-horizontal">
    <h4>ConcremoteDevice</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.id)
    @Html.HiddenFor(model => model.DeviceConfig_id)
    @Html.HiddenFor(model => model.Device_statustypes_id)
    @Html.HiddenFor(model => model.ConcremoteDevice.id)
相关问题