在MVC中使用模型编辑POST

时间:2015-10-07 08:33:20

标签: c# model-view-controller controller

    // GET: /Winches/Edit/5
    public async Task<ActionResult> Edit(int? id)
    {
        WinchesBrand winchesbrand = await db.WinchesBrands.FindAsync(id);

        var model = new WinchModel
        {
            WinchBrandId = winchesbrand.WinchBrandId,
            WinchBrandName = winchesbrand.WinchBrandName,
            RopeList = new List<int?>() { }
        };

        foreach (var rope in winchesbrand.Ropes)
        {
            model.RopeList.Add(rope.RopeId);
        }
        if (model.RopeList.Any() == false)
        {
            model.RopeList.Add(null);
        }
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        ViewBag.RopeList = db.Ropes.Where(e => e.IsDeleted == false).ToList();
        return View(model);
    }
抱歉我的英文, 我不知道,怎么写Post for this Edit

这是我的变种:

    [HttpPost]
    public async Task<ActionResult> Edit(WinchModel model)
    {
        if (ModelState.IsValid)
        {
            List<Rope> ropesList = new List<Rope>();
            WinchesBrand winch = new WinchesBrand 
            {
                WinchBrandName = model.WinchBrandName,
                Ropes = ropesList

            };
            //db.WinchesBrands.Where(w => w.WinchBrandName == model.WinchBrandName)
            //    .Update();

            foreach (var ropeId in model.RopeList.Where(w => w > 0))
            {
                db.Ropes.Find(ropeId).WinchesBrand = winch;
            }

            if (model.RopeList.Any() == false)
            {
                model.RopeList.Add(null);
            }
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        ViewBag.RopeList = new SelectList(db.Ropes.Where(e => e.IsDeleted == false), "RopeId", "RopeName");
        return View(model);
    }

但这不刷新(我不知道这个命令)

{db.WinchesBrands.Where(w => w.WinchBrandName == model.WinchBrandName)
            //    .Update();}

P.C。我刚开始学习这个

2 个答案:

答案 0 :(得分:0)

在保存更改之前添加此内容

db.Entry(model).State = EntityState.Modified;

它会将您的输入标记为已修改,EF会更新它。

答案 1 :(得分:0)

 if (ModelState.IsValid)
        {
            WinchesBrand winch = new WinchesBrand
            {
                WinchBrandId = model.WinchBrandId.Value,
                WinchBrandName = model.WinchBrandName
            };
            db.Entry(winch).State = EntityState.Modified;

            var ropeToDelete = db.Ropes
                .Where(r => r.IdWinch == model.WinchBrandId 
                    && !model.RopeList.Contains(r.RopeId))
                    .ToList();
            foreach(var rope in ropeToDelete){
                rope.IdWinch = null;
            }

            foreach (var ropeId in model.RopeList.Where(w => w > 0))
            {
                var rope = new Rope { RopeId = ropeId.Value };
                db.Ropes.Attach(rope);
                rope.IdWinch = winch.WinchBrandId;
            }

            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

这是正确的