首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MVC中使用模型进行编辑的帖子

在MVC中使用模型进行编辑的帖子
EN

Stack Overflow用户
提问于 2015-10-07 16:33:20
回答 2查看 26关注 0票数 0
代码语言:javascript
复制
    // 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);
    }

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

这是我的变体:

代码语言:javascript
复制
    [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);
    }

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

代码语言:javascript
复制
{db.WinchesBrands.Where(w => w.WinchBrandName == model.WinchBrandName)
            //    .Update();}

P.C.我刚开始学这个

EN

回答 2

Stack Overflow用户

发布于 2015-10-07 16:53:13

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

代码语言:javascript
复制
db.Entry(model).State = EntityState.Modified;

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

票数 0
EN

Stack Overflow用户

发布于 2015-10-07 20:37:23

代码语言:javascript
复制
 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");
        }

这是正确的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32987435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档