简体   繁体   中英

Use array to add new entry in model in asp.net mvc

I'm using asp.net mvc 4 and Entity Framework 6 to make a website where I can store data in MSSQL database. I want to make a function where it'll make a copy of an entry with different id. I want to put those copied values in an array and push it to the model to make the back-end processing faster. But I'm not sure how to do it.

Here are my codes:

    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" });
        }
    }

How can I put the values in an array and push the array in model to add a new entry?

You could detach the entity from the DbContext then re-add it to the EntityCollection.

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

solution 2:(is better)

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

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