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

时间:2016-09-27 12:35:12

标签: c# asp.net asp.net-mvc razor

每次我尝试编辑我在ASP.NET MVC项目中创建的记录时,单击“保存”时字段都将无效。

以下是我的编辑获取和在我的控制器中发布的代码的一部分:

审核控制器:

// 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")
        }

非常感谢帮助

提前致谢

1 个答案:

答案 0 :(得分:1)

请不要使用开发电脑来检查,但是您确定要查看其中的数据。如果有,您能确定以下行有效吗?

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");
}

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