2015-08-25 14 views
0

首先,这个问题的名称不是很好,但我想不出一个更好的。 我正在开展一个项目,我每天都会报道不同的项目。asp.net c#控制器代理怪异

这是它的样子。 enter image description here

但现在的问题是,如果我将它提交它会像这样结束。

enter image description here

就像我们可以在这里看到在每月的总结,是对谢胜利,迄今为止,它复制该项目,并显示它的两倍,这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(); 
    } 
任何不同势行为
+1

调试这个好的第一步就是将某些组件从你的Action中分离出来,分成更小的方法。一种方法应该有一个单一的责任;这将使它们更易于测试并且更易于读取/调试。 –

+0

我知道现在有什么问题,我已经设置好了,所以我可以为我设置设置,例如开始时间,启动项目等,如果我在设置中添加启动项目,它将以这种方式进行操作。但如果我手动按下添加项目,同时报告时间,它会像它应该做的那样工作。 –

回答

0

它wasen't做怪,在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中移除。