在视图中编辑记录时,数据库表中的字段无效

本文关键字:字段 数据库 无效 视图 编辑 记录 | 更新日期: 2023-09-27 18:08:58

每次我尝试编辑我在ASP中创建的记录时。. NET MVC项目,当我单击save时,字段无效。

这是我的编辑Get和post在我的控制器代码的一部分:

检查控制器:

// GET: Reviews/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Review review = db.Reviews.Find(id);
        if (review == null)
        {
            return HttpNotFound();
        }
    ViewBag.ReviewID = new SelectList(db.Reviews, "ReviewID", "Rating", review.ReviewID);
    return View(review);
}

// POST: Reviews/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ReviewID,Username,WellnessService,Rating,Feedback,Date")] Review review)
{
    if (ModelState.IsValid)
    {
        db.Entry(review).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("ReviewEdit");
    }
    ViewBag.ReviewID = new SelectList(db.Reviews, "ReviewID", "Rating", review.ReviewID);
    return View(review);
}

下面是Edit视图的代码:

@using (Html.BeginForm())
        {
                @Html.AntiForgeryToken()
            <div class="form-horizontal">
                <h4>@Html.DisplayFor(model => model.WellnessService)</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.ReviewID)

                <div class="form-group">
                    @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10" align="left">
                        @{
                            List<SelectListItem> listItems = new List<SelectListItem>();
                            listItems.Add(new SelectListItem { Text = "1", Value = "1" });
                            listItems.Add(new SelectListItem { Text = "2", Value = "2" });
                            listItems.Add(new SelectListItem { Text = "3", Value = "3" });
                            listItems.Add(new SelectListItem { Text = "4", Value = "4" });
                            listItems.Add(new SelectListItem { Text = "5", Value = "5" });
                            listItems.Add(new SelectListItem { Text = "6", Value = "6" });
                            listItems.Add(new SelectListItem { Text = "7", Value = "7" });
                        }
                        @Html.DropDownListFor(model => model.Rating, listItems, "Please choose value")
                        @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.Feedback, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.Feedback, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Feedback, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Save" class="btn btn-default" />
                    </div>
                </div>
            </div>
                            }
        <div>
            @Html.ActionLink("Back to My Reviews", "ReviewEdit", new { @class = "btn btn-success btn-lg" })
        </div>
        @section Scripts {
            @Scripts.Render("~/bundles/jqueryval")
        }

非常感谢您的帮助

Thanks in advance

在视图中编辑记录时,数据库表中的字段无效

不要有一个开发pc来检查这个,但你确定审查有数据在里面。如果有,你能确定下面的代码行能正常工作吗?

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

你不需要把评论和上下文联系起来吗?

如果您临时执行类似;

if (ModelState.IsValid)
{
    var foo = db.Entry.Where(x=>x.reviewId == review.reviewId).First();
    // then manually set all the parameters
    db.SaveChanges();
    return RedirectToAction("ReviewEdit");
}

这并不优雅,但如果它工作,并且您的数据被更新,至少您知道这是您将对象附加到上下文的方式。当我可以得到一个开发工作站时,我会尝试得到一个更好的例子。