简体   繁体   English

在mvc项目中插入错误:{“对象引用未设置为对象的实例。”}

[英]Insertion in mvc project error: {“Object reference not set to an instance of an object.”}

i am working in mvc i want do save the details.. here i have a error.. controller 我在MVC中工作,我想保存详细信息..这里我有一个错误..控制器

public ActionResult Edit(FormCollection frm)
    {
        StaffDetails sdtl=new StaffDetails();
        int UnvId = int.Parse(TempData["UnvID1"].ToString());
        sdtl.UnvId = UnvId;
        //sdtl.UnvId = int.Parse(TempData["UnvID"].ToString());
        string SportsName = frm["SportsAffliation"];
        var SpotsID = Db.SportsCodes.Where(s => s.SportsName == SportsName).FirstOrDefault();
        sdtl.SportsID = SpotsID.SportsCodeID;
        sdtl.Name = frm["name"];
        sdtl.Address.Street = frm["street"];
        sdtl.Address.City = frm["city"];
        sdtl.Address.State = frm["state"];
        sdtl.Address.PostalCode = frm["zip"];
        sdtl.Address.Country = frm["country"];
        sdtl.Email = frm["Email"];
        sdtl.Phone = frm["Phone"];
        sdtl.SportsAffliation = frm["SportsAffliation"];
        Db.Entry(sdtl).State = System.Data.Entity.EntityState.Modified;
        Db.SaveChanges();
        //return RedirectToAction("Edit", new { id = cohid, tab = 0 });
        return View();
    }

Domain

 public class StaffDetails
    {
        [Key]
        public int StaffId { get; set; }
        public int UnvId { get; set; }
        public University University { get; set; }
        public int SportsID { get; set; }
        public SportsCode SportsCode { get; set; }
        public int SportsGroupId { get; set; }
        public SportsGroup SportsGroup { get; set; }
        public int CoachingStaffId { get; set; }
        public CoachingStaff CoachingStaff { get; set; }
        public string Name { get; set; }
        public string SportsAffliation { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string OrganizationalLeader { get; set; }
        public Address Address { get; set; }
    }

error in below line from controller.. error only from sdtl.address... 从控制器的下面一行错误..仅来自sdtl.address的错误...

sdtl.Address.Street = frm["street"];
    sdtl.Address.City = frm["city"];
    sdtl.Address.State = frm["state"];
    sdtl.Address.PostalCode = frm["zip"];
    sdtl.Address.Country = frm["country"];

error is {"Object reference not set to an instance of an object."} 错误是{“对象引用未设置为对象的实例。”}

i thing this a logical problem but i can't find because of i am new in mvc..can you solve this problem 我认为这是一个逻辑问题,但由于我是mvc的新手而无法找到..你能解决这个问题吗

You can either new the address object up in the constructor: 您可以在构造函数中新建地址对象:

public StaffDetails()
{
    Address = new Address();
}

Or new address up in the Edit action in the controller. 或在控制器的“编辑”操作中添加新地址。

public ActionResult Edit(FormCollection frm)
{
    StaffDetails sdtl=new StaffDetails() { Address = new Address() };
    // Rest of code 
}

This way would mean that you have to new up the Address object each time you use StaffDetails. 这种方式意味着您每次使用StaffDetails时都必须重新创建Address对象。

Add a constructor to your class and initialize the Address proeperty: 在您的类中添加一个构造函数并初始化Address属性:

public StaffDetails()
{
    Address = new Address();
}

i think you should make a constructor for StaffDetails. 我认为您应该为StaffDetails构造一个。 in constructor you should initialize objects like address. 在构造函数中,您应该初始化诸如地址之类的对象。 i mean when you use StaffDetails sdtl=new StaffDetails(); 我的意思是当您使用StaffDetails sdtl=new StaffDetails(); this line it creates an StaffDetails object but Address object inside sdtl is still null. 此行将创建一个StaffDetails对象,但sdtl中的Address对象仍为null。

you should create new instance from Address in Constractor StaffDetails : 您应该从承包商StaffDetails中的Address创建新实例:

public class StaffDetails
{

    public StaffDetails()
    {
      this.Address = new Address();
    }

    [Key]
    public int StaffId { get; set; }
    public int UnvId { get; set; }
    public University University { get; set; }
    public int SportsID { get; set; }
    public SportsCode SportsCode { get; set; }
    public int SportsGroupId { get; set; }
    public SportsGroup SportsGroup { get; set; }
    public int CoachingStaffId { get; set; }
    public CoachingStaff CoachingStaff { get; set; }
    public string Name { get; set; }
    public string SportsAffliation { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string OrganizationalLeader { get; set; }
    public Address Address { get; set; }
}

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

相关问题 你调用的对象是空的。 MVC - object reference not set to an instance of an object. MVC 错误“对象引用未设置为对象的实例。” - Error 'Object reference not set to an instance of an object.' 你调用的对象是空的。 错误 - Object reference not set to an instance of an object. Error 你调用的对象是空的。 错误 - Object reference not set to an instance of an object. Error Object 引用未设置为 object 的实例。 MVC 和外键 - Object reference not set to an instance of an object. MVC and Foreign Key MVC:通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。”错误 - MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error ASP.NET MVC“'对象引用未设置为 object 的实例。'”错误 - ASP.NET MVC "'Object reference not set to an instance of an object.'" Error MVC4部分视图错误“对象引用未设置为对象的实例。” - MVC4 Partial View Error “Object reference not set to an instance of an object.” 部分视图错误未将对象引用设置为对象的实例。 MVC4剃刀 - Partial view error Object reference not set to an instance of an object. MVC4 Razor SqlConnection出现错误“对象引用未设置为对象的实例。” - Error “Object reference not set to an instance of an object.” for SqlConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM