asp.net c# Controller acting weird -
first of, name of question not not think of better one. i'm working project report hours perday, , on diffrent projects.
but problem now, if submit it end this.
like can see here on summary of month, on secound date duplicates project , shows twice, happends if add more dates well. but never happends on first date.
i've been debugging it, not see diffrent behavior when ran first time, values same etc.
this controller
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 dates being sent view , put string. string input = (form["date"]); input = input.trim(); string[] dates = input.split(' '); //foreach loop on dates, rebuilds string yy-mm-dd can parse 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 x.contains("_") select x.substring(x.indexof('_'))).distinct(); projectdata.times = new timereporttimes(form["starttime"], form["endtime"], form["breaktime"], modelstate); projectdata.date = reportdate; //checks see if did choose project. var enumerable = projectnumbers 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 hours 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 worked hours bigger 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 report database projectdata.savetodatabase(constants.currentuser(user.identity.name)); //saves viewdata true know in 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(); }
it wasen't strange, @ end of foreach loop had if statement
if (modelstate.isvalid) { projectdata.savetodatabase(constants.currentuser(user.identity.name)); viewdata["posted"] = true; //loads default settings , projects. projectdata = new newtimereportmodel(); loaddefaultsettings(projectdata); }
this last loaddefaultsettings(projectdata);
loads settings again, after saved first date, did load settings again next date. there got duplicated. did move out of foreach.
Comments
Post a Comment