I'm trying to develop a website using asp.net mvc 4
& EF6
where I want to update record and add new record to another model using the values from updated record. So far I can update the existing record but I get error in the controller when system is trying to add new record to another model. The error is,
Object reference not set to an instance of an object.
Here are my codes,
Controller
[HttpPost]
public ActionResult RentController(FlatModel flatId)
{
if (Session["AdminNAME"] != null)
{
if (ModelState.IsValid)
{
var dbPost = rentdb.FlatInfoes.Where(p => p.flatno == flatId.Flats.flatno).FirstOrDefault();
if (dbPost == null)
{
return RedirectToAction("RentController");
}
dbPost.flat_owner_name = flatId.Flats.flat_owner_name;
dbPost.flat_owner_phone = flatId.Flats.flat_owner_phone;
var addRentSchedule = flatId.BillCheck;
addRentSchedule.fullname = flatId.Flats.flat_owner_name; //error showing in this line.
addRentSchedule.isRented = "N";
addRentSchedule.due = 10000;
DateTime today = DateTime.Now;
DateTime newDay = today.AddDays(30);
addRentSchedule.submitTime = newDay;
rentdb.BillPayChecks.Add(addRentSchedule);
rentdb.SaveChanges();
TempData["flat_assign_success"] = "Information Updated Successfully!";
return RedirectToAction("RentController");
}
else
{
TempData["flat_assign_fail"] = "Error! Information update failed!";
return RedirectToAction("RentController");
}
}
else
{
return RedirectToAction("AdminLogin");
}
}
Model
public class FlatModel
{
public FlatInfo Flats { get; set; }
public BillPayCheck BillCheck { get; set; }
}
Am I doing something wrong in my code? How can I add the record from the updated values when updating the model? Need this help badly. Tnx.