I have a create controller on ASP Net core application, that creates a new FinancePaymentReceipt
.
The controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("FinanceInvoiceID,Payment_Reference_Number,FinancePaymentTypeID,Payment_Amount,Payment_Date")] FinancePaymentReceipt financePaymentReceipt)
{
var invoice = await _context.FinanceInvoices
.Include(fi=>fi.FinanceDailyClaim)
.ThenInclude(fdc=>fdc.Department)
.Where(ss=>ss.FinanceInvoiceID==financePaymentReceipt.FinanceInvoiceID)
.SingleOrDefaultAsync();
if (invoice == null)
{
return NotFound();
}
if (ModelState.IsValid)
{
financePaymentReceipt.PaymentReceiptNumber = await _numberSequence.GetNumberSequence("RECPT", invoice.FinanceDailyClaim.Department.Department_Code, 'C');
_context.Add(financePaymentReceipt);
await _context.AddAsync(new FinanceAccountSatement
{
FinancePaymentReceiptID = financePaymentReceipt.FinancePaymentReceiptID,
Activity_Date = financePaymentReceipt.Date_Captured
});
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
The model
public class FinancePaymentReceipt
{
private decimal _Payment_Amount;
[Key]
public int FinancePaymentReceiptID { get; set; }
[Display(Name = "Payment Receive Number")]
public string PaymentReceiptNumber { get; set; }
[Display(Name = "Invoice Number")]
public int FinanceInvoiceID { get; set; }
[Display(Name = "Payment Reference No")]
public string Payment_Reference_Number { get; set; }
[Display(Name = "Payment Type")]
public int FinancePaymentTypeID { get; set; }
public decimal Payment_Amount
{
get { return _Payment_Amount; }
set
{
_Payment_Amount = -Math.Abs(value);
}
}
public string Description
{
get
{
return $"Payment for {FinanceInvoice.Invoice_Number} Thank You";
}
}
public DateTime Payment_Date { get; set; }
public string Captured_By { get; set; }
public DateTime Date_Captured { get; set; } = DateTime.Now;
public FinanceInvoice FinanceInvoice { get; set; }
public FinancePaymentType FinancePaymentType { get; set; }
}
From the FinancePaymentReceipt
model above i have a property Description
and it has only the get
accesor and retrieves the Invoice number from the FinanceInvoice
model.
But the problem is that when i create it, tries to get the Description
before saving the model to the database, In fact if I put a break point before at the start of the create controller
it does not even hit.
i just get the error Object reference not set to an instance of an object
when i click the 'create button`
Is there a way around this issue?