0

所以在我的个人表中......我有 Id、Name 和 HolidaysRemaining。

它用于假期预订应用程序,当用户从日历中选择日期并单击按钮时,atm 选择的每个日期都将存储在数据库中,我试图将剩余的假期减去 1,因为每个假期都已预订,但是它似乎没有好转。

//listHolidays in correct format dd/mm/yy
    [HttpPost]
    public ActionResult listHolidays(Holiday holiday, Person person , int? PersonId, string HolidayDate, string endDate, string AlreadyExists)
    {
        db.People.Attach(person);
        //int holidaysRemaining = 20;
        //person.HolidaysRemaining = holidaysRemaining;    

        DateTime startDates = Convert.ToDateTime(HolidayDate);
        DateTime endDates = Convert.ToDateTime(endDate);

    try{

        while (startDates <= endDates)
        {
            if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
            {                 
                //if user selects Holiday that already exists, wont add it to Db
                //gets string, and uses the previously converted to dateTime 'startDate'
                //id so only applies to person creating holidays
                ViewBag.CantDuplicateHolidays = String.IsNullOrEmpty(AlreadyExists) ? "date" : "";
                var dates = from d in db.Holidays
                where d.HolidayDate == startDates && d.PersonId == PersonId
                select d;

                // <= 0..so if holiday does not already exist
                if (dates.Count() <= 0)
                {
                  //  holidaysRemaining--;
                    person.HolidaysRemaining = person.HolidaysRemaining - 1;

                    Holiday holiday1 = new Holiday();
                    holiday1.PersonId = PersonId.Value;
                    holiday1.HolidayDate = startDates;

                    db.Holidays.AddObject(holiday1);


                    db.SaveChanges();
                    //say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
                    //but doesnt chage the value of startdates = 'startdates'
                }

            }
        }

        startDates = startDates.AddDays(1);
    }


    finally
    {
        db.People.Detach();
    }

    return RedirectToAction("Index");
}
4

2 回答 2

1

我认为你的问题在这里:

if (dates.Count() <= 0)
    {
    //  holidaysRemaining--;
    person.HolidaysRemaining--;

尝试将其更改为:

if (dates.Count() <= 0)
    {
    //  holidaysRemaining--;
    person.HolidaysRemaining = person.HolidaysRemaining - 1;

编辑

此外,您从未真正使用人员更新数据库?

db.People.Attach(person);

db.SaveChanges();

再次编辑

尝试这个:

[HttpPost] public ActionResult listHolidays(Holiday holiday, Person person, int? PersonId, string HolidayDate, string endDate, string AlreadyExists) {
//int holidayRemaining = 20; //person.HolidaysRemaining = holidayRemaining;

        DateTime startDates = Convert.ToDateTime(HolidayDate);
        DateTime endDates = Convert.ToDateTime(endDate);

        while (startDates <= endDates)
        {
            if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)
            {                 
                    //if user selects Holiday that already exists, wont add it to Db
                    //gets string, and uses the previously converted to dateTime 'startDate'
                    //id so only applies to person creating holidays
                    ViewBag.CantDuplicateHolidays = String.IsNullOrEmpty(AlreadyExists) ? "date" : "";
                    var dates = from d in db.Holidays
                    where d.HolidayDate == startDates && d.PersonId == PersonId
                    select d;

                    // <= 0..so if holiday does not already exist
                    if (dates.Count() <= 0)
                    {
                      //  holidaysRemaining--;
                        person.HolidaysRemaining = person.HolidaysRemaining - 1;

                        Holiday holiday1 = new Holiday();
                        holiday1.PersonId = PersonId.Value;
                        holiday1.HolidayDate = startDates;

                        db.Holidays.AddObject(holiday1);
            db.People.Attach(person);

                        db.SaveChanges();
                        //say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
                        //but doesnt chage the value of startdates = 'startdates'
                    }

            }

            startDates = startDates.AddDays(1);
        }

        return RedirectToAction("Index");
    }
于 2012-12-13T16:59:42.777 回答
1

可能这是最简单的解决方案。

代替:

person.HolidaysRemaining = person.HolidaysRemaining - 1;

和:

var dbPerson = from p in db.People where p.Id == PersonId select p;
dbPerson[0].HolidaysRemaining--;

或者,我们正在讨论附加 person 对象,因为您拥有它:

    db.People.Attach(person)
    try {
        // ... loop and everything else here
    } finally {
        db.People.Detach(person);
    }
} // end of method

但这有点脆弱,只有Persondb.People.

注意:两者personPersonId都被传递到listHolidays().

于 2012-12-13T20:36:55.567 回答