首先,这个问题的名称并不是很好,但我想不出更好的问题。 我正在做一个项目,我每天报告小时数,以及不同的项目。
但问题是现在,如果我提交它,它最终会像这样。
就像我们可以在这个月的摘要中看到的那样,它在第二个日期重复项目并显示两次,如果我添加更多日期,这种情况就会发生。 但它从未在第一次约会发生。
我一直在调试它,但是我没有看到第一次运行它时的任何不同行为,所有的值都是相同的等等。
这是控制器
public ActionResult TimeReport(FormCollection form, Guid? id)
{
ViewDataDictionary vd = new ViewDataDictionary
{
["projects"] = new DatabaseLayer().GetConsultantProjects(Constants.CurrentUser(User.Identity.Name)),
["id"] = 1,
["showDescription"] = true
};
ViewData["vd"] = vd;
NewTimeReportModel projectData = new NewTimeReportModel();
if (form != null && form.AllKeys.Contains("delete"))
{
new DatabaseLayer().DeleteTimeReport(Guid.Parse(form["ReportId"]));
LoadDefaultSettings(projectData);
ViewData.Model = projectData;
return View();
}
if (id.HasValue && (form == null || form.AllKeys.Length == 0))
{
using (DatabaseLayer db = new DatabaseLayer())
{
var timeReport = db.GetTimeReport(id.Value);
projectData = new NewTimeReportModel(timeReport);
if (projectData.Projects.Count == 1)
projectData.Projects[0].Hours = null;
}
}
else if (form == null || form.AllKeys.Length == 0)
{
LoadDefaultSettings(projectData);
}
else
{
//Takes all the dates that is being sent from the view and put into a string.
string input = (form["date"]);
input = input.Trim();
string[] dates = input.Split(' ');
//Foreach to loop over all the dates, also rebuilds the string to look like yy-mm-dd so we can parse it to a datetime
foreach (string result in dates.Select(date => date.Substring(0, 2) + '-' + date.Substring(2, 2) + "-" + date.Substring(4, 2)))
{
DateTime reportDate;
if (!DateTime.TryParse(result, out reportDate))
{
ModelState.AddModelError("Date", "Felaktikt datum");
}
var projectNumbers = (from x in form.AllKeys
where x.Contains("_")
select x.Substring(x.IndexOf('_'))).Distinct();
projectData.Times = new TimeReportTimes(form["startTime"], form["endTime"], form["breakTime"], ModelState);
projectData.Date = reportDate;
//Checks to see if we did choose a project.
var enumerable = projectNumbers as string[] ?? projectNumbers.ToArray();
if (!enumerable.Any())
{
ModelState.AddModelError("Projekt", "Inga projekt valda...");
}
else
{
int emptyHours = 0;
foreach (string projectNumber in enumerable)
{
projectData.Projects.Add(new NewTimeReportModel.Project
{
Description = form["description" + projectNumber],
Hours = null,
ProjectId = Guid.Parse(form["project" + projectNumber])
});
string hourString = form["hours" + projectNumber];
if (string.IsNullOrEmpty(hourString))
{
emptyHours++;
projectData.Projects[projectData.Projects.Count - 1].Hours =
projectData.Times.WorkedHours;
}
else
{
if (!projectData.Projects[projectData.Projects.Count - 1].SetHours(hourString))
{
ModelState.AddModelError("hours_" + projectNumber, "Felaktig antal timmar");
}
}
}
//Checks so the hours are right.
if (emptyHours > 1 || (emptyHours == 0 && projectData.Projects.Sum(x => x.Hours) != projectData.Times.WorkedHours))
{
ModelState.AddModelError("hours_" + enumerable.First(),
"Antalet timmar stämmer ej överrens");
}
//Checks so the worked hours is bigger than 0
if (projectData.Projects.Any(x => x.Hours <= 0))
{
ModelState.AddModelError("hours_" + enumerable.First(),
"Antalet timmar måste vara större än noll");
}
if (!string.IsNullOrEmpty(form["ReportId"]))
{
projectData.ReportId = Guid.Parse(form["ReportId"]);
}
if (ModelState.IsValid)
{
//saves the report to the database
projectData.SaveToDatabase(Constants.CurrentUser(User.Identity.Name));
//Saves ViewData To true so we know in the view it's been posted.
ViewData["posted"] = true;
projectData = new NewTimeReportModel();
LoadDefaultSettings(projectData);
}
else if (projectData.Projects.Count == 1)
{
projectData.Projects[0].Hours = null;
}
}
}
}
var missingdays = new DatabaseLayer().GetConsultantMissingDays(Constants.CurrentUser(User.Identity.Name));
if (missingdays.Count == 0)
{
}
else
{
ViewBag.MissingDays = missingdays;
}
ViewData.Model = projectData;
return View();
}
答案 0 :(得分:0)
它没什么奇怪的,在foreach循环的最后我有这个if语句
if (ModelState.IsValid)
{
projectData.SaveToDatabase(Constants.CurrentUser(User.Identity.Name));
ViewData["posted"] = true;
//Loads default settings and projects.
projectData = new NewTimeReportModel();
LoadDefaultSettings(projectData);
}
这最后像LoadDefaultSettings(projectData);
再次加载所有设置,因此在保存第一个日期后,它确实再次为下一个日期加载所有设置。因为他们得到了重复。所以我确实把它从foreach中移开了。