public ActionResult Create() { // Get bill amount from request params -- Change to ModelState.IsValid later string _amount = Request.Form["amount"]; float amount = 0; // Change this to redirect with errors if (float.TryParse(_amount, out amount) == false) return Redirect("Index"); // Init new bill Bill bill = new Bill(); bill.Amount = amount; bill.OwnerId = getUserIdString(); bill.GroupId = 1; // -- Replace with actual group ID later bill.isPaid = false; bill.Type = "Electricity"; // Add to db db.Bills.Add(bill); db.SaveChanges(); return Redirect("Index"); //float amount = float.Parse(Request.Form["amount"]); /*if (ModelState.IsValid) { bill.OwnerId = getUserIdString(); //bill.GroupId = getGroupId(); bill.isPaid = false; db.Bills.Add(bill); db.SaveChanges(); //ViewBag.Success = true; return Redirect("Index"); } else { // Decision: // Pass error list to ViewBag here, OR figure how to use @Html to take bill argument //ViewBag.Success = false; return Redirect("Index"); }*/ //return View(); }
public ActionResult CreateNew() { // Get the count param and membership string int count = int.Parse(Request.Form["count"]); string membersString = Request.Form["membersString"]; // Get group id int? groupID = getLoggedInUser().groupID; if (groupID == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); // Split up the membership string and count it char[] delemiters = {'|'}; string[] memberIDs = membersString.Split(delemiters, StringSplitOptions.RemoveEmptyEntries); int count2 = memberIDs.Count(); // Throw error if counts don't match if (count != count2) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); float amount = float.Parse(Request.Form["amount"]); // Create new bill Bill bill = new Bill(); bill.Amount = amount; bill.GroupId = groupID.Value; bill.isPaid = false; bill.Type = "Electricity"; bill.OwnerId = getUserIdString(); // Add to db db.Bills.Add(bill); db.SaveChanges(); foreach (string memId in memberIDs) { // Check this user ID string is in DB bool validID = db.Users.Any(x => x.Id == memId); // Invalid member string if (!validID) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } else { // Get the split from params hash string _portion = Request.Params[memId]; float portion = float.Parse(_portion); // Calculate chunk float chunk = portion / amount; // Init new bill split BillSplit bs = new BillSplit(); bs.MemberId = memId; bs.groupID = groupID.Value; bs.Paid = false; bs.Portion = chunk; bs.BillId = bill.id; // Add split to DB db.BillSplits.Add(bs); db.SaveChanges(); } } return Redirect("/bills/index"); }