public async Task<ActionResult> Edit(int id, Expense expense) { try { var client = ExpenseTrackerHttpClient.GetClient(); // create a JSON Patch Document JsonPatchDocument<DTO.Expense> patchDoc = new JsonPatchDocument<DTO.Expense>(); patchDoc.Replace(e => e.Amount, expense.Amount); patchDoc.Replace(e => e.Date, expense.Date); patchDoc.Replace(e => e.Description, expense.Description); // serialize and PATCH var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc); var response = await client.PatchAsync("api/expenses/" + id, new StringContent(serializedItemToUpdate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return RedirectToAction("Details", "ExpenseGroups", new { id = expense.ExpenseGroupId }); } else { return Content("An error occurred"); } } catch { return Content("An error occurred"); } }
public async Task<ActionResult> Create(Expense expense) { try { var client = ExpenseTrackerHttpClient.GetClient(); // serialize & POST var serializedItemToCreate = JsonConvert.SerializeObject(expense); var response = await client.PostAsync("api/expenses", new StringContent(serializedItemToCreate, System.Text.Encoding.Unicode, "application/json")); if (response.IsSuccessStatusCode) { return RedirectToAction("Details", "ExpenseGroups", new { id = expense.ExpenseGroupId }); } else { return Content("An error occurred"); } } catch { return Content("An error occurred"); } }
private void SetNewExpense(int expenseGroupId) { Expense = new Expense() { ExpenseGroupId = expenseGroupId }; }
public Entities.Expense CreateExpense(Expense expense) { return new Entities.Expense { Amount = expense.Amount, Date = expense.Date, Description = expense.Description, ExpenseGroupId = expense.ExpenseGroupId, Id = expense.Id }; }
private void SetExpense(DTO.Expense expense) { // We should not use the same expense instance, but a new one Expense = new Expense() { Amount = expense.Amount, Date = expense.Date, Description = expense.Description, ExpenseGroupId = expense.ExpenseGroupId, Id = expense.Id }; }
public object CreateDataShapedObject(Expense expense, List<string> lstOfFields) { if (!lstOfFields.Any()) { return expense; } // create a new ExpandoObject & dynamically create the properties for this object var objectToReturn = new ExpandoObject(); foreach (string field in lstOfFields) { // need to include public and instance, b/c specifying a binding flag overwrites the // already-existing binding flags. object fieldValue = expense.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expense, null); // add the field to the ExpandoObject ((IDictionary<string, object>) objectToReturn).Add(field, fieldValue); } return objectToReturn; }
public SetExpenseToEditMessage(Expense expense) { Expense = expense; }
public ActionResult Edit(int id, Expense expense) { return View(); }
public ActionResult Create(Expense expense) { return View(); }