简体   繁体   中英

Object reference not set to an instance of an object

l am current working on a Mvc 4 application. 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

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. If l step Over, only the Name is returning a value the rest return me a nulls on ViewModels. May anyone help why my viewModels are nulls. Thank you in advance.

  1. What is OfficeCreateModels in the signature of the method Create ? You posted only the OfficeCreateModel class.
  2. What is the variable models in the body of the method? In the body of method, only the variable model is used (and declared in the signature of method).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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