简体   繁体   中英

Update record and add new record in another model using updated values in asp.net mvc

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.

This is a little bit of a "troubleshoot my code" type question, so getting an accurate answer will be tough. However, looking at this line

var addRentSchedule = flatId.BillCheck;

Makes me think you are getting a Null reference. You are seeing the error on the line right after this one because you are trying to set properties on a null object. If the rent schedule is infact a NEW database record, then most likely, you need to create a new object. So, your code possibly should look something like this:

var addRentSchedule = new BillCheck();
//you need to tie this to the parent record using the parent record
//primary key and setting the BillCheck Foreign key appropriately
//since I dont know your db schema, I am guessing on this line.
addRentSchedule.flatId = flatId.Id;
addRentSchedule.fullname = flatId.Flats.flat_owner_name;
addRentSchedule.isRented = "N";
addRentSchedule.due = 10000;
//no point in creating variables when you can do it all in one line here.
addRentSchedule.submitTime = DateTime.Now.AddDays(30);
rentdb.BillPayChecks.Add(addRentSchedule);

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