0
首先,這個問題的名稱不是很好,但我想不出一個更好的。 我正在開展一個項目,我每天都會報道不同的項目。asp.net c#控制器代理怪異
但現在的問題是,如果我將它提交它會像這樣結束。
就像我們可以在這裏看到在每月的總結,是對謝勝利,迄今爲止,它複製該項目,並顯示它的兩倍,這happends如果我添加更多的日期。 但是它從未在第一次約會中發生。
我已經調試它,但我沒有看到它的時候跑了第一次,所有的值相同等
這是控制器
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();
}
任何不同勢行爲
調試這個好的第一步就是將某些組件從你的Action中分離出來,分成更小的方法。一種方法應該有一個單一的責任;這將使它們更易於測試並且更易於讀取/調試。 –
我知道現在有什麼問題,我已經設置好了,所以我可以爲我設置設置,例如開始時間,啓動項目等,如果我在設置中添加啓動項目,它將以這種方式進行操作。但如果我手動按下添加項目,同時報告時間,它會像它應該做的那樣工作。 –