C# “创建控制器”;对象引用未设置为对象的实例;
我在ASP Net核心应用程序上有一个创建控制器,它创建一个新的C# “创建控制器”;对象引用未设置为对象的实例;,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我在ASP Net核心应用程序上有一个创建控制器,它创建一个新的FinancePaymentReceive 控制器 [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create([Bind("FinanceInvoiceID,Payment_Reference_Number,FinancePaymentTypeID,Payment_Amount,Payment_Date")] Financ
FinancePaymentReceive
控制器
[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));
}
从上面的financepaymentreceive
模型中,我有一个属性Description
,它只有get
accesor,并从FinanceInvoice
模型中检索发票号
但问题是,当我创建它时,在将模型保存到数据库之前尝试获取描述
,事实上,如果我在创建控制器
的开始处放置断点,它甚至不会命中
当我单击“创建”按钮时,我只得到错误对象引用未设置为对象的实例`
有办法解决这个问题吗?一种解决方案是,如果尚未设置财务发票
,则只需为说明
返回null
。大概是这样的:
public string Description
{
get
{
return FinanceInvoice == null
? null
: $"Payment for {FinanceInvoice.Invoice_Number} Thank You";
}
}
我正在使用C#在那里。尝试将[NotMapped]
添加到不在数据库中的属性。
public string Description
{
get
{
return FinanceInvoice == null
? null
: $"Payment for {FinanceInvoice.Invoice_Number} Thank You";
}
}