I have gone through lot of posts however unable to resolve this issue.
- I input some values on the View and click the Submit button
- My repo calculates the payment schedule based on the criteria and displays the list (that's what I am trying to achieve)
This is where I am having issues. On the initial load my Model is null and I get the null reference exception.
I have just started coding with asp so please ignore the basics in this case. Any suggestions would be helpful
@model FinanceApplication.ViewModels.LoanViewModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div>
<table style="width: 35%;" class="style1">
<tr>
<td>Enter Price</td>
<td>@Html.TextBoxFor(model => model.Amount)</td>
</tr>
<tr>
<td>Enter Deposit</td>
<td>@Html.TextBoxFor(model => model.Deposit)</td>
</tr>
<tr>
<td>Finance Option</td>
<td>
@Html.DropDownListFor(model =>
model.Term, new SelectList(Enum.GetValues(typeof(Options))),
"Select Option",
new { @class = "form-control" })
</td>
</tr>
<tr>
<td>Delivery Date</td>
<td>
@Html.EditorFor(model => model.Deliverydate)
</td>
</tr>
<tr>
<td>Arrangement Fee</td>
<td>@Html.TextBoxFor(model => model.ArrangementFee)</td>
</tr>
<tr>
<td>Completion Fee</td>
<td>@Html.TextBoxFor(model => model.CompletionFee)</td>
</tr>
<tr>
<td>
<input type="submit" id="btnSubmit"
value="Submit" onclick="myFunction();" />
</td>
</tr>
</table>
</div>
<div id="loan-form" class="text-left">
<h1>Payment Schedule</h1>
<table>
<tr>
<td>Amount (£) </td>
<td>@Html.DisplayFor(model => model.Amount)</td>
</tr>
<tr>
<td>Loan Amount (£) </td>
<td>@Html.DisplayFor(model => model.LoanAmount)</td>
</tr>
<tr>
<td>Deposit (£) </td>
<td>@Html.DisplayFor(model => model.Deposit)</td>
</tr>
<tr>
<td>Term (years) </td>
<td>@Html.DisplayFor(model => model.Term)</td>
</tr>
<tr>
<td>Delivery Date</td>
<td>@Html.DisplayFor(model => model.Deliverydate)</td>
</tr>
<tr>
<td>
<table class="table table-striped">
<thead>
<tr>
<th>Payment Date</th>
<th>Payment Amount (£) </th>
<th>Total Paid (£) </th>
<th>Balance (£) </th>
</tr>
</thead>
<tbody>
if(Model != null)
{
@foreach (var item in Model.PaymentSchedule)
{
<tr>
<td> @Html.DisplayFor(modelItem =>
item.PaymentDate) </td>
<td> @Html.DisplayFor(modelItem =>
item.PaymentAmount) </td>
<td> @Html.DisplayFor(modelItem =>
item.TotalPaid) </td>
<td> @Html.DisplayFor(modelItem =>
item.RemainingBalance) </td>
</tr>
}
}
else
{
<tr>
<td>No Items found</td>
</tr>
}
</tbody>
</table>
</td>
</tr>
</table>
</div>
}
{
private IPaymentScheduleRepository _paymentScheduleRepository;
public HomeController(IPaymentScheduleRepository paymentScheduleRepository)
{
_paymentScheduleRepository = paymentScheduleRepository; }
[HttpGet]
public IActionResult Index()
{ return View(new LoanViewModel()); }
[HttpPost]
public async Task<IActionResult> Index(LoanViewModel requestParameters)
{ LoanViewModel output = await _paymentScheduleRepository.GetPaymentSchedule(requestParameters);
return View("Index", output);}}
//ViewModel
public class LoanViewModel
{
[Required(ErrorMessage = "Please enter the loan amount.")]
[Range(1, 100000000,
ErrorMessage = "Loan amount must be between 1,000 and 1000,000.")]
public decimal Amount { get; set; }
[Required(ErrorMessage = "Please enter the deposit amount.")]
[Remote(action: "VerifyDeposit", controller:"Home")]
public decimal Deposit { get; set; }
[Required(ErrorMessage = "Please select the term of the loan.")]
public string Term { get; set; }
[Required(ErrorMessage = "Please select the delivery date.")]
[DataType(DataType.Date)]
[Display(Name = "Delivery Date")]
[DisplayFormat(ApplyFormatInEditMode = true,
DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime Deliverydate { get; set; }
public decimal ArrangementFee { get; set; }
public decimal CompletionFee { get; set; }
public decimal LoanAmount
{
get { return (Amount - Deposit); }
}
[Display(Name = "Payment Schedule")]
public List<SheduledPayments> PaymentSchedule { get; set; }
}
//PaymentSchedule
public class SheduledPayments
{
public DateTime PaymentDate { get; set; }
public decimal PaymentAmount { get; set; }
public decimal RemainingBalance { get; set; }
public decimal TotalPaid { get; set; }
}