使用 Mailkit 的 asp.net 核心 mvc 应用程序中选定的下拉值的 NULLReferenceException

NULLReferenceException for a selected drop-down value in an asp.net core mvc application using Mailkit

我正在开发一个 asp.net 核心 5.0 MVC 应用程序,我使用 Mailkit Nuget 包在创建包含所有答案的记录后向客户端发送电子邮件。我已将与 Mailkit 相关的代码放在 Create(Post) 方法中,但是在创建和发送电子邮件后,我在我的 Mailkit 代码部分中获得了 System.NullReferenceException 用于我的字段之一,当我编写时称为 Product Type它像 sale.ProductType.ProductTypeDesc,它基本上是一个下拉菜单,有一个查找 table,因此是主 table(Sale) 的外键。但它正在与 sale.ProductTypeId 一起工作,这不是我想要的。谁能给我解决方案?

这是我的模型:

使用系统;

使用 System.Collections.Generic;

#nullable 禁用

命名空间Sale_app.Models

{

public partial class Sale

{
    public int SaleId { get; set; }

    public string ProductName { get; set; }

    public string ProductTypeId { get; set; }
    

    public virtual ProductType ProductType { get; set; }
 
}

}

使用系统;

使用 System.Collections.Generic;

#nullable 禁用

命名空间Sale_app.Models

{ public 部分 class 产品类型

{
    public ProductType()

    {

        Sales = new HashSet<Sale>();

    }

    public int ProductTypeId { get; set; }

    public string ProductTypeDesc { get; set; }

    public virtual ICollection<Sale> Sales { get; set; }

}

}

这些是我的控制器中的创建方法:

//获取销售

public IActionResult 创建()

    {

        ViewBag.ProductTypeList = _context.ProductType.ToList();

        return View();
    }

[HttpPost]

    [ValidateAntiForgeryToken]

    public IActionResult Create([Bind("SaleId,ProductTypeId,ProductName")] Sale sale)

    {
        
        if (ModelState.IsValid)
        {
            try
            {
                _context.Add(sale);
                _context.SaveChanges();

                //instantiate a new MimeMessage
                var message = new MimeMessage();
                //Setting the To e-mail address
                message.To.Add(new MailboxAddress("E-mail Recipient Name", "an email address comes here"))
                //Setting the From e-mail address
                message.From.Add(new MailboxAddress("Sale Form " + "Response", "an email address comes here"));
                //E-mail subject 
                message.Subject = "Sale Form";
                //E-mail message body
                message.Body = new TextPart(TextFormat.Html)
                {
                    Text = "<b>SaleId:</b> " + sale.SaleId + "<br/>" + "<b>ProductName:</b> " + sale.ProductName + "<br/>" +
                    "<b>ProductType</b>: " + sale.ProductType.ProductTypeDecs

                };

                //Configure the e-mail
                using (var emailClient = new SmtpClient())
                {
                    emailClient.Connect("smtp.gmail.com", 587, false);
                    emailClient.Authenticate("an email address comes here", "a password comes here");
                    emailClient.Send(message);
                    emailClient.Disconnect(true);
                }
                return RedirectToAction(nameof(Index));

            }

            catch (Exception ex)
            {
                ModelState.Clear();
                ViewBag.Message = $" Oops! We have a problem here {ex.Message}";
            }
        }


        return View(sale);
       
    }

更改代码:


_context.Add(sale);
_context.SaveChanges();

var newSale= _context.Sales
.Include(i=> i.ProductType)
.Where(i=> i.SaleId=sale.SaleId)
.FirstOrDefault();

......
......
 message.Body = new TextPart(TextFormat.Html)
  {
   Text = "<b>SaleId:</b> " + newSale.SaleId + "<br/>" + "<b>ProductName:</b> " + newSale.ProductName + "<br/>" +
"<b>ProductType</b>: " + newSale.ProductType.ProductTypeDecs
 };