创建控制器“对象引用未设置为对象的实例”

时间:2019-07-23 16:52:49

标签: c# entity-framework asp.net-core

我在ASP Net核心应用程序上有一个创建控制器,该控制器创建一个新的FinancePaymentReceipt

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));
}

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; }
}

在上面的FinancePaymentReceipt模型中,我有一个属性Description,它只有get附件,并从FinanceInvoice模型中检索发票编号。

但是问题是,当我创建模型时,尝试在将模型保存到数据库之前获取Description,实际上,如果我在create controller的开头放置了一个断点,甚至没有命中。

当我单击“创建”按钮时,我只会收到错误Object reference not set to an instance of an object

Image when creating Error after button click

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

一种解决方案是,如果尚未设置if(object typeOf String){} else if (object typeOf CustomDto ){} else if (object typeOf PatientDto){} ,则为null返回Description。像这样:

FinanceInvoice

我在这里使用C#的conditional operater