所以在我的人桌上...我有Id,Name& HolidaysRemaining。
它用于假日预订应用程序,并且当用户从日历中选择日期并单击按钮时,所选择的每个日期将存储在数据库中,我试图减去剩余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");
}
答案 0 :(得分: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(假日假日,Person person,int?PersonId,string HolidayDate,string endDate,string AlreadyExists)
{
// int holidaysRemaining = 20;
//person.HolidaysRemaining = holidaysRemaining;
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");
}
答案 1 :(得分:1)
这可能是最简单的解决方案。
取代:
person.HolidaysRemaining = person.HolidaysRemaining - 1;
使用:
var dbPerson = from p in db.People where p.Id == PersonId select p;
dbPerson[0].HolidaysRemaining--;
或者我们讨论了附加人物对象,因为你拥有它:
db.People.Attach(person)
try {
// ... loop and everything else here
} finally {
db.People.Detach(person);
}
} // end of method
但这有点脆弱,只有在Person
中还没有db.People
对象时才有必要。
注意:将person
和PersonId
传递给listHolidays()
似乎有点奇怪。