简体   繁体   English

你调用的对象是空的

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

l am current working on a Mvc 4 application. 我目前正在研究Mvc 4应用程序。 My create Controller is as follows. 我的创建控制器如下。

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

My ViewModels and DataModels are as follows respectively 我的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; }      
}

When l'm running a code it goes on to create an Id and Name entities the rest of the entities return null. 当我运行代码时,它将继续创建Id和Name实体,其余实体返回null。 If l step Over, only the Name is returning a value the rest return me a nulls on ViewModels. 如果l结束,则仅Name返回一个值,其余返回ViewModels上的null。 May anyone help why my viewModels are nulls. 谁能帮助我为什么viewModels为null。 Thank you in advance. 先感谢您。

  1. What is OfficeCreateModels in the signature of the method Create ? Create方法的签名中的OfficeCreateModels是什么? You posted only the OfficeCreateModel class. 您只发布了OfficeCreateModel类。
  2. What is the variable models in the body of the method? 方法主体中的变量models是什么? In the body of method, only the variable model is used (and declared in the signature of method). 在方法主体中,仅使用变量model (并在方法签名中声明)。

I would have a look at the variables you're using. 我将看看您使用的变量。 It looks like that you have a NullReference exception because of these variables not initialized. 由于未初始化这些变量,因此您似乎具有NullReference异常。

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

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