i am working in mvc i want do save the details.. here i have a error.. controller
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.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
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.
Add a constructor to your class and initialize the Address
proeperty:
public StaffDetails()
{
Address = new Address();
}
i think you should make a constructor for StaffDetails. in constructor you should initialize objects like address. i mean when you use StaffDetails sdtl=new StaffDetails();
this line it creates an StaffDetails object but Address
object inside sdtl is still null.
you should create new instance from Address in Constractor StaffDetails :
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; }
}
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.