Luca
Luca

Reputation: 288

Can't save data into multiple tables using ASP.NET EF6

On a page on my website a person can change information about a device.

This data comes from 2 different tables. Saving data into the DeviceStatus table is no problem.

But for some reason I can't save the Active field into the concremodeDevice table. Saving all other data from this table is no problem.

Code:

Controller:

[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);
}

Page:

@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>

Table:

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

So my question is if someone knows why I can't save Active.

As suggested by pinkfloydx33 in the comments I have at some point also tried to set the state of concremodeDevice, but this gave me the next error:

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, don't know if ASP.NET of EF6 has anything to do with this, but included it just to be sure.

Upvotes: 1

Views: 79

Answers (1)

Roland_dfa
Roland_dfa

Reputation: 58

Try the following:

Controller:

[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);
        }

And view:

<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)

Upvotes: 2

Related Questions