//Adds a CostCenter to CostCenter_Relation public ContractCreateCostsViewModel AddCostCenter(ContractCreateCostsViewModel model) { var cRelation = new ContractCostCenter_Relation(); cRelation.ContractId = model.ContractId; //Sum all Percentages and calculate new init Value double allPercentages = 0; foreach (ContractCostCenter_Relation tempRelation in model.ContractCostCenter_Relations) { allPercentages += tempRelation.Percentage; } cRelation.Percentage = 1 - allPercentages; model.ContractCostCenter_Relations.Add(cRelation); int last = model.ContractCostCenter_Relations.Count - 1; var CostCenterList = new SelectList(db.CostCenters, "Id", "Description"); model.CostCenterSelectLists.Add(CostCenterList); model.CostCenterIds.Add(null); model.CostCenterPercentages.Add(null); //Repeat Model Initialization of SelectLists -> See GET: ActionMethod model = CreateCostsHelper(model); //initialization:end return(model); }
//********************************************************************************************************************************* // GET: Contract/Create/CreateCosts public ActionResult CreateCosts(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //David Create and populate the ViewModel var model = new ContractCreateCostsViewModel(); //Set the Contract Id !!before initialization with Helper model.ContractId = (int)id; model = CreateCostsHelper(model); return(View(model)); }
//Removes a CostCenter in CostCenter_Relation by index public ContractCreateCostsViewModel RemoveCostCenter(ContractCreateCostsViewModel model, int index) { var relations = model.ContractCostCenter_Relations; int last = model.ContractCostCenter_Relations.Count - 1; //Set the last elemt to the deleted ones; model.ContractCostCenter_Relations[index] = model.ContractCostCenter_Relations[last]; model.CostCenterSelectLists[index] = model.CostCenterSelectLists[last]; model.CostCenterIds[index] = model.CostCenterIds[last]; model.CostCenterPercentages[index] = model.CostCenterPercentages[last]; //Then delet the last elements → we make sure, that no entry keeps empty model.ContractCostCenter_Relations.RemoveAt(last); model.CostCenterSelectLists.RemoveAt(last); model.CostCenterIds.RemoveAt(last); model.CostCenterPercentages.RemoveAt(last); //Repeat Model Initialization of SelectLists -> See GET: ActionMethod model = CreateCostsHelper(model); //initialization:end return(model); }
//CreateCostsHelper public ContractCreateCostsViewModel CreateCostsHelper(ContractCreateCostsViewModel model) { //Define Contract, which is currently loaded Contract contract = db.Contracts.Find(model.ContractId); if (contract != null) { //Decide, if contract in DB or the current model have the updated Data and select this one with data in it - or, if both have data, use model. model.ContractValue = (model.ContractValue != null) ? model.ContractValue : contract.ContractValue; model.AnnualValue = (model.AnnualValue != null) ? model.AnnualValue : contract.AnnualValue; model.PaymentBegin = (model.PaymentBegin != null) ? model.PaymentBegin : contract.PaymentBegin; model.PaymentInterval = (model.PaymentInterval != null) ? model.PaymentInterval : contract.PaymentInterval; model.Tax = (model.Tax != null) ? model.Tax : ((contract.Tax != null) ? contract.Tax : 1.19);; model.PrePayable = (model.PrePayable == true) ? true : ((contract.PrePayable != null) ? (bool)contract.PrePayable : false); model.VarPayable = (model.VarPayable == true) ? true : ((contract.VarPayable != null) ? (bool)contract.VarPayable : false); model.Adaptable = (model.Adaptable == true) ? true : ((contract.Adaptable != null) ? (bool)contract.Adaptable : false); //Initialize CostCenterIds if (model.ContractCostCenter_Relations == null) { //Case: The model has no Relation → model Ids must set and SelectLists must set; model.CostCenterIds = new List <int?>(); model.CostCenterPercentages = new List <double?>(); model.CostCenterSelectLists = new List <IEnumerable <SelectListItem> >(); if (contract.ContractCostCenter_Relations != null) { //Case if contract in db has already a Relation → The Relations can be assumed. model.ContractCostCenter_Relations = contract.ContractCostCenter_Relations.ToList(); //For each Relation, create an Id, where you can set the selected value later. int j = 0; foreach (ContractCostCenter_Relation cCenter in contract.ContractCostCenter_Relations) { model.CostCenterIds[j] = cCenter.Id; model.CostCenterPercentages[j] = cCenter.Percentage; j++; } } else { //Case: no Relations exists, so it is initialized with an empty one; model.ContractCostCenter_Relations = new List <ContractCostCenter_Relation>(); } } //And finally the Kinds again model.CostKindId = (model.CostKindId != null) ? model.CostKindId : ((contract.CostKind != null) ? (int?)contract.CostKind.Id : null); } //Else case not necessary, because, if no contract exists nothing can happen //Enum does not need Dropdownlist initialization: Runtimetypes int i = 0; foreach (ContractCostCenter_Relation cCenter in model.ContractCostCenter_Relations) { model.CostCenterSelectLists[i] = new SelectList(db.CostCenters, "Id", "Description", model.CostCenterIds[i]); i++; } //Selectlist initialization model.CostKinds = new SelectList(db.CostKinds, "Id", "Description", model.CostKindId); model.Clients = new SelectList(db.Clients, "Id", "ClientName"); //Add Contract to View to display its information in following Forms (like Status and Name) model.Contract = contract; if (model.CostCenterIndex == null) { model.CostCenterIndex = -1; } //Save in TempData, because HiddenFore doesn't support Lists or they must be saved in a foreach - loop which is very unefficient TempData["ContractCostCenter_Relations"] = model.ContractCostCenter_Relations; TempData["CostCenterIds"] = model.CostCenterIds; TempData["CostCenterSelectLists"] = model.CostCenterSelectLists; TempData["CostCenterPercentages"] = model.CostCenterPercentages; return(model); }
public ActionResult CreateCosts(ContractCreateCostsViewModel model, string submit) { //Initialize the Lists, which aren't brought by HiddenFor's or Form-Data model.ContractCostCenter_Relations = (List <ContractCostCenter_Relation>)TempData["ContractCostCenter_Relations"]; model.CostCenterIds = (List <int?>)TempData["CostCenterIds"]; model.CostCenterSelectLists = (List <IEnumerable <SelectListItem> >)TempData["CostCenterSelectLists"]; model.CostCenterPercentages = (List <double?>)TempData["CostCenterPercentages"]; //The option to add a CostCenter on submit if (submit == "addCostCenter") { model = AddCostCenter(model); //model = CreateCostsHelper(model); not necessary, because done in Remove-Method return(View(model)); } //Option to remove the selected Option brought by model.CostCenterIndex else if (submit == "removeCostCenter") { model = RemoveCostCenter(model, (int)model.CostCenterIndex); //model = CreateCostsHelper(model); not necessary, because done in Remove-Method return(View(model)); } //The normal Validation if (ModelState.IsValid) //If all values are accepted { //load contract var contract = db.Contracts.Find(model.ContractId); //set contract from model contract.ContractValue = model.ContractValue; contract.AnnualValue = model.AnnualValue; contract.PaymentBegin = model.PaymentBegin; contract.PaymentInterval = model.PaymentInterval; contract.Tax = model.Tax; contract.PrePayable = model.PrePayable; contract.VarPayable = model.VarPayable; contract.Adaptable = model.Adaptable; //CostCenter contract.ContractCostCenter_Relations = new List <ContractCostCenter_Relation>(); int i = 0; foreach (ContractCostCenter_Relation cCenter in model.ContractCostCenter_Relations) { if (model.CostCenterIds[i] != null) { cCenter.CostCenterId = (int)model.CostCenterIds[i]; cCenter.Percentage = (double)model.CostCenterPercentages[i]; contract.ContractCostCenter_Relations.Add(cCenter); } i++; } //and finally CostKind contract.CostKind = db.CostKinds.Find(model.CostKindId); //Set Contract Status from HelperUtility contract.ContractStatus = HelperUtility.checkContractStatus(contract, db); db.Entry(contract).State = EntityState.Modified; db.SaveChanges(); //Decide which button was pressed...then redirect if (submit == continueBtn) { return(RedirectToAction("CreateFiles", new { id = contract.Id })); } else { return(RedirectToAction("Index")); } } //Repeat Model Initialization of SelectLists -> See GET: ActionMethod model = CreateCostsHelper(model); //initialization:end return(View(model)); }