【问题标题】:POST for Edit with model in MVC在 MVC 中使用模型进行编辑的 POST
【发布时间】:2015-10-07 08:33:19
【问题描述】:
    // 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);
    }

对不起我的英语, 我不知道,如何为这个编辑写帖子

这是我的变种:

    [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();}

PC我刚开始学习这个

【问题讨论】:

    标签: c# model-view-controller controller


    【解决方案1】:

    在保存更改之前添加它

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

    它将您的条目标记为已修改,EF 将对其进行更新。

    【讨论】:

    • 实体类型 WinchModel 不是当前上下文模型的一部分。
    • 创建数据库后是否更新了模型类?您是否执行了迁移,您的数据库中是否有包含 WinchModel 的表?
    • 我更新了模型类,但没有执行任何迁移。我的数据库中有 WinchModel 表。
    • 好吧,尝试执行迁移和更新数据库。您的例外“实体类型 WinchModel 不是当前上下文模型的一部分。”表示 EF 已连接到数据库,但没有找到任何与模型相关的表,您要执行编辑。
    【解决方案2】:
     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");
            }
    

    这是正确的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多