繁体   English   中英

你调用的对象是空的

[英]Object reference not set to an instance of an object

我目前正在研究Mvc 4应用程序。 我的创建控制器如下。

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Create(OfficeCreateModels model)
{
    bool success = false;
    string message = "";

    try
    {
       if (model.Name.IsNullOrWhitespace()) throw new Exception("Unable to create this Office Name. The Type cannot be blank or spaces.");

       var company = models.Companies.FirstOrDefault(x => x.Id == model.CompanyId);

       var officeExist = models.Offices.Any(x => x.Name.ToLower() == model.Name.ToLower() && x.CompanyId == model.CompanyId);

       if (officeExist) 
                throw new Exception(string.Format("Company '{0}' already has an Office this named '{1}'" .FormatWith(company.Name.ToUpperCase(), model.Name.ToUpperCase())));

       Office office = new Office
       {
          Name = model.Name.ToUpperCase(),
          Address1 = model.Address1.ToUpperCase(),
          Address2 = model.Address2.ToUpperCase(),
          Address3 = model.Address3.ToUpperCase(),
          Telephone = model.Telephone.ToUpperCase(),
          Fax = model.Fax.ToUpperCase(),
          Email = model.Email.ToUpperCase(),
          AccountType = model.AccountType.ToUpperCase()
       };

       models.Offices.Add(office);
       models.SaveChanges();

       success = true;
       return RedirectToAction("Index");
        }
    catch (Exception ex)
    {
       message = ex.Message;
    }

    return View(model);
}

我的ViewModels和DataModels分别如下

public class OfficeCreateModel
{
   public Guid Id { get; set; }
   public string Name { get; set; }
   public string Address1 { get; set; }
   public string Address2 { get; set; }
   public string Address3 { get; set; }
   public string Telephone { get; set; }
   public string Fax { get; set; }
   public string Email { get; set; }

   public Guid CompanyId { get; set; }
   public bool IsDeleted { get; set; }
}


//DataModels
public class Office
{
   [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public Guid Id { get; set; }
   [MaxLength(50)]
   public string Name { get; set; }
   [MaxLength(100)]
   public string Address1 { get; set; }
   [MaxLength(100)]
   public string Address2 { get; set; }
   [MaxLength(100)]
   public string Address3 { get; set; }
   [MaxLength(20)]
   public string Telephone { get; set; }
   [MaxLength(20)]
   public string Fax { get; set; }
   [MaxLength(255)]
   public string Email { get; set; }

   public Guid CompanyId { get; set; }
   public virtual Company Company { get; set; }
   public virtual List<Agent> Agents { get; set; }      
}

当我运行代码时,它将继续创建Id和Name实体,其余实体返回null。 如果l结束,则仅Name返回一个值,其余返回ViewModels上的null。 谁能帮助我为什么viewModels为null。 先感谢您。

  1. Create方法的签名中的OfficeCreateModels是什么? 您只发布了OfficeCreateModel类。
  2. 方法主体中的变量models是什么? 在方法主体中,仅使用变量model (并在方法签名中声明)。

我将看看您使用的变量。 由于未初始化这些变量,因此您似乎具有NullReference异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM