GuardianZ
GuardianZ
CC#
Created by GuardianZ on 3/12/2024 in #help
Unable to calculate/store correct value to the db
it does I think, but realised in the mean time I am being completely dumb as usual. I haven't invoked the method anywhere or on any action, so I decided to call the method when I hit the submit button on the form. The idea is that it saves the form data to the db and then invokes the method to perform calculation on it but now I am getting a completely new unhandled exception which I don't understand at the moment, but working on it: this is my main controller code for submitting the form now:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SubmitForm([Bind("Id,OutstandingMortgageBalance,RepaymentTerm,CurrentInterestRate,CurrentMonthlyPayment")] MortgageForm mortgageForm)
{
if (ModelState.IsValid)
{
_context.Add(mortgageForm);
await _context.SaveChangesAsync();

// call method in InterestRateController to calculate and save daily interest rate
await _interestRateController.SaveDailyInterestRate();

return RedirectToAction(nameof(Index));
}
return View(mortgageForm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SubmitForm([Bind("Id,OutstandingMortgageBalance,RepaymentTerm,CurrentInterestRate,CurrentMonthlyPayment")] MortgageForm mortgageForm)
{
if (ModelState.IsValid)
{
_context.Add(mortgageForm);
await _context.SaveChangesAsync();

// call method in InterestRateController to calculate and save daily interest rate
await _interestRateController.SaveDailyInterestRate();

return RedirectToAction(nameof(Index));
}
return View(mortgageForm);
}
It seems the current issue might be that I am injecting controller with my method into another controller, instead of having a service for this? Edit: I haven't registered my service in Program.cs file, all good now! 🙂
4 replies
CC#
Created by GuardianZ on 3/12/2024 in #help
Unable to calculate/store correct value to the db
And my main controller below that stores the calculated value back into db:
namespace Finance101.Controllers
{
public class InterestRateController : Controller
{
private readonly DailyInterestRateService _dailyInterestRateService;
private readonly Finance101Context _context;

public InterestRateController(DailyInterestRateService dailyInterestRateService, Finance101Context context)
{
_dailyInterestRateService = dailyInterestRateService;
_context = context;
}

[HttpPost]
public async Task<IActionResult> SaveDailyInterestRate()
{
try
{
//retrieve calculated daily interest rate from the service
decimal dailyInterestRate = await _dailyInterestRateService.CalculateDailyInterestRate();
//create a new instance of the MortgageForm model
var mortgageForm = new MortgageForm
{
DailyInterestRate = dailyInterestRate
};

//save the daily interest rate to the database
_context.MortgageForm.Add(mortgageForm);
await _context.SaveChangesAsync();

return Ok();
}
catch (Exception ex)
{
//ILogger.LogError(ex, "Error saving daily interest rate");
return StatusCode(500, ex);
}
}
}
}
namespace Finance101.Controllers
{
public class InterestRateController : Controller
{
private readonly DailyInterestRateService _dailyInterestRateService;
private readonly Finance101Context _context;

public InterestRateController(DailyInterestRateService dailyInterestRateService, Finance101Context context)
{
_dailyInterestRateService = dailyInterestRateService;
_context = context;
}

[HttpPost]
public async Task<IActionResult> SaveDailyInterestRate()
{
try
{
//retrieve calculated daily interest rate from the service
decimal dailyInterestRate = await _dailyInterestRateService.CalculateDailyInterestRate();
//create a new instance of the MortgageForm model
var mortgageForm = new MortgageForm
{
DailyInterestRate = dailyInterestRate
};

//save the daily interest rate to the database
_context.MortgageForm.Add(mortgageForm);
await _context.SaveChangesAsync();

return Ok();
}
catch (Exception ex)
{
//ILogger.LogError(ex, "Error saving daily interest rate");
return StatusCode(500, ex);
}
}
}
}
4 replies