C# - 对象值正在丢失

时间:2017-09-24 11:39:00

标签: c# asp.net asp.net-mvc

我试图通过这种方法将我的对象放入另一个: -

'use strict';

module.exports = function AppError(message, httpStatus) {
  Error.captureStackTrace(this, this.constructor);
  this.name = this.constructor.name;
  this.message = message;
  this.status = httpStatus;
};

require('util').inherits(module.exports, Error);

我的问题是在这行代码中: -

for (int i = 0; i < NewTblList.Count; i++)
{
    var ItObj = NewTblList[i];

    InternShip It = new InternShip();
    It.Name = NewTblList[i].Name;
    It.Amount = NewTblList[i].Amount;
    It.CreatedDate = NewTblList[i].CreatedDate;
    It.Descr = NewTblList[i].Descr;
    It.Duration = NewTblList[i].Duration;
    It.ExpiryDate = NewTblList[i].ExpiryDate;
    It.StartDate = NewTblList[i].StartDate;

    It = db.Interns.FirstOrDefault(x => x.ID == ItObj.ID);
    results = UtilityMethods<InternShip, int>.EditEntity(db, It);
}

我的'它'价值已经丢失。我无法弄清楚我做错了什么。请帮助!!

1 个答案:

答案 0 :(得分:1)

您正在使用It数据库中存在的任何内容Interns覆盖您的ItObj.ID

所以你需要把它改成:

foreach (var internship in NewTblList)
{
    // get the existing internship from the database
    InternShip It = db.Interns.FirstOrDefault(x => x.ID == internship.ID);

    // update the values from your list
    It.Name = internship.Name;
    It.Amount = internship.Amount;
    It.CreatedDate = internship.CreatedDate;
    It.Descr = internship.Descr;
    It.Duration = internship.Duration;
    It.ExpiryDate = internship.ExpiryDate;
    It.StartDate = internship.StartDate;

    results = UtilityMethods<InternShip, int>.EditEntity(db, It);
}