コード例 #1
0
        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");
            }
        }
コード例 #2
0
        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");
            }
        }
コード例 #3
0
 private void SetNewExpense(int expenseGroupId)
 {
     Expense = new Expense()
         {
             ExpenseGroupId = expenseGroupId
         };
 }
コード例 #4
0
 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
     };
 }
コード例 #5
0
        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
                };
        }
コード例 #6
0
        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;
        }
コード例 #7
0
 public SetExpenseToEditMessage(Expense expense)
 {
     Expense = expense;
 }
コード例 #8
0
        public ActionResult Edit(int id, Expense expense)
        {
             
            return View();

        }
コード例 #9
0
 public ActionResult Create(Expense expense)
 {
     return View();
 }