使用数组在asp.net mvc中的模型中添加新条目

时间:2016-07-20 06:11:01

标签: arrays asp.net-mvc entity-framework

我正在使用asp.net mvc 4entity framework 6创建一个网站,我可以在MSSQL db中存储数据。我想创建一个函数,它将复制一个具有不同id的条目。我想将这些复制的值放在一个数组中并将其推送到模型以使后端处理更快。但我不知道该怎么做。这是我的代码,

    public ActionResult FlatCopy(FlManagement FlTable, int FlId = 0)
    {
        var getOwner = rentdb.OwnerRegs.Where(a => a.username == User.Identity.Name).FirstOrDefault();
        var getId = getOwner.serial;
        var getLimit = getOwner.limit;
        var getPosted = getOwner.posted;

        FlInfo model = FlTable.Flats;
        if (ModelState.IsValid)
        {
            if (getLimit != 0)
            {
                try
                {
                    getOwner.limit = getLimit - 1;
                    getOwner.posted = getPosted + 1;
                    var dbPost = rentdb.FlatInfoes.Where(p => p.serial == FlId).FirstOrDefault();
                    if (dbPost == null)
                    {
                        TempData["flat_edit_fail"] = "Error! Information update failed!";
                        return RedirectToAction("FlatManager", new { FlPanelId = "AllFl" });
                    }
                    model.flatno = "Copy of " + dbPost.flatno;
                    model.type = dbPost.type;
                    model.owner_id = getId;
                    model.t_id = 0;
                    model.t_name = "N/A";
                    model.t_phone = "N/A";
                    model.basic = dbPost.basic;
                    model.electric = dbPost.electric;
                    model.gas = dbPost.gas;
                    model.water = dbPost.water;
                    model.total = dbPost.total;
                    model.advancerent = dbPost.advancerent;
                    model.currency = dbPost.currency;
                    model.title = dbPost.title;
                    model.status = "N/A";
                    db.FlatInfoes.Add(model);
                    db.SaveChanges();

                    TempData["success"] = "Information Added Successfully!";
                    return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
                }
                catch
                {
                    TempData["fail"] = "Error! Please Provide Valid Information.";
                    return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
                }
            }
            else
            {
                TempData["fail"] = "You've reached your post limit!";
                return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
            }
        }
        else
        {
            TempData["fail"] = "Error! Please Provide Valid Information.";
            return RedirectToAction("FlManager", new { FlPanelId = "AllFl" });
        }
    }

如何将值放在数组中并在模型中推送数组以添加新条目?急需这个帮助!感谢。

1 个答案:

答案 0 :(得分:1)

您可以从DbContext中分离实体,然后将其重新添加到EntityCollection中。

rentdb.Entry(dbPost).State = EntityState.Detached;
rentdb.FlatInfoes.Add(dbPost);

解决方案2 :(更好)

var model = new FlInfo();
rentdb.FlatInfoes.Add(model);
var sourceValues = rentdb.Entry(dbPost).CurrentValues;
rentdb.Entry(model).CurrentValues.SetValues(sourceValues);
rentdb.SaveChanges();