简体   繁体   English

使用asp.net mvc中的更新值更新记录并在另一个模型中添加新记录

[英]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. 我正在尝试使用asp.net mvc 4EF6开发一个网站,我想在其中更新记录,并使用更新记录中的值将新记录添加到另一个模型中。 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. 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. 让我认为您正在获取Null参考。 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. 如果租期表实际上是一个NEW数据库记录,则很可能需要创建一个新对象。 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);

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

相关问题 ASP.NET MVC 在创建新用户时向表中添加另一条记录 - ASP.NET MVC add another record to a table when creating a new user 如何使用ASP.NET MVC和KnockoutJS很好地添加记录? - How to add record well using ASP.NET MVC and KnockoutJS? 身份模型中的实体框架ASP.NET MVC6,创建新记录,而不是将其链接到现有记录 - Entity Framework ASP.NET MVC6 in Identity Model, Creating a new record instead of linking it to existing record 检查记录是否存在然后更新,否则在 ASP.NET MVC 中插入新记录 - Check if a record exists then update, otherwise insert new record in ASP.NET MVC 在一个表中创建记录将更新另一个表中的现有记录 ASP.NET Core MVC - Creating a record in one table will update an existing record in another table ASP.NET Core MVC 通过asp.net mvc中的模型绑定更新和删除记录 - updating and deleting record through model binding in asp.net mvc 使用条件 ASP.NET MVC 将多条记录插入一个模型 - Insert multiple record into one model with condition ASP.NET MVC 在asp.net mvc中删除记录后如何更新视图 - How to update view after deleting record in asp.net mvc 如何在我的ASP.NET MVC 5项目中使用LINQ LAMBDA更新记录 - How to update a record using LINQ LAMBDA in my ASP.NET MVC 5 Project 如何使用 C# 在 ASP.NET MVC 中将记录添加到数据库中? - How to add a record into the database in ASP.NET MVC using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM