首页 > 解决方案 > 您无法在 ASP.NET 中保存评论

问题描述

我有我上次从代码中删除的审查控制代码,public ActionResult Edit ([Bind (Exclude = "Name")]我应该能够编辑和更改审查,不幸的是,我找不到问题。

    [Authorize]
    public class ReviewsController : BaseController
    {
        // GET: Reviews
        public ActionResult Index([Bind (Prefix ="Id")]int productId)
        {
            var product = _db.Products.Find(productId);

            if(product != null)
            {
                return View(product);
            }

            return HttpNotFound();
        }
        public ActionResult Create(int productId)
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Review model)
        {
            if(ModelState.IsValid)
            {
                model.Name = User.Identity.Name;
                _db.Reviews.Add(model);
                _db.SaveChanges();

                return RedirectToAction("Index", new { id = model.ProductId });
            }
            return View(model);
        }
        public ActionResult Edit (int id)
        {
            var review = _db.Reviews.Find(id);

            if (review.Name == User.Identity.Name)
            {
                if (review != null)
                {
                    return View(review);
                }
            }
            return RedirectToAction("Index", "Products");
        }
        [HttpPost]
        public ActionResult Edit(Review model)
        {
            if (model.Name == User.Identity.Name)
            {
                if (ModelState.IsValid)
                {
                    _db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    _db.SaveChanges();

                    return RedirectToAction("Index", new { id = model.ProductId });
                }
            }
            return View(model);
        }
    }

以下是您要编辑和保存评论的地方

在此处输入图像描述

我需要做什么才能编辑评论并保存。

我应该在这段代码的哪里更改某些内容或其他地方可能有问题?

标签: c#asp.net-mvc

解决方案


推荐阅读