ASP.NET MVC 编辑模型的最初未给定值的部分 // db.saveChanges() 不起作用

ASP.NET MVC editing parts of model which is not initially given values // db.saveChanges() doesn't work

我在 ASP.NET MVC 应用程序中创建了一个带有控制器和视图的模型。最初,当使用创建操作创建新应用程序时,用户无法填写模型的 5 个部分(参见代码)。 只有以管理员身份登录时才能进行编辑,而不能以任何用户身份登录。将正确的应用程序的编辑页面连接到正确的用户不是问题。但是,当我填写复选框并在 'edit mode' 中写下任何评论等,然后按提交时,没有任何反应。程序似乎没有注册更改(db.SaveChanges() 不起作用??)

请问有人知道我该如何解决这个问题,或者首先请问问题是什么?好像保存按钮只是一个 shell,所以我可能有连接或缺少某些东西?

感谢您的宝贵时间。

代码:

部分型号(后5个未填写'create mode'

[Display(Name = "Course for master")]
        public string Course_Master { get; set; }
       
        [Required]
        [Display(Name = "Words for Office")]
        [MaxLength(3000)]
        public string Motivation { get; set; }

        //[DataType(DataType.Upload)]
        [Display(Name = "Upload Resume")]
        //[Required(ErrorMessage = "Please choose file to upload")]
        public string Resume { get; set; }

//these 5 are the ones I want to edit
        public bool Interview { get; set; } //checkbox
        public string Comments { get; set; }
        public string Notes { get; set; }
        public bool Unfit { get; set; } //checkbox
        public bool Candidate { get; set; } //checkbox

剃须刀页面

@model NEA.Models.Application
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit</h2>
@using (Html.BeginForm("Edit","Applications", FormMethod.Post))
{
    @Html.AntiForgeryToken()
<div class="form-horizontal">
    <h4>Application</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.UserId)
    <div class="form-group">
        @Html.LabelFor(model => model.Interview, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Interview)
                @Html.ValidationMessageFor(model => model.Interview, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Comments, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Unfit, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Unfit)
                @Html.ValidationMessageFor(model => model.Unfit, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Candidate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.Candidate)
                @Html.ValidationMessageFor(model => model.Candidate, "", new { @class = "text-danger" })
            </div>
        </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 List", "Index")
</div>

动作

// GET: Applications/Edit/5
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Application application = db.Applications.Find(id);
            if (application == null)
            {
                return HttpNotFound();
            }
            return View(application);
        }
// POST: Applications/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Application application)
        {
            if (ModelState.IsValid)
            {
                db.Entry(application).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(application);
        }

编辑

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Application application)
        {
                //db.Entry(application).State = EntityState.Modified;
                db.Entry(application).Property(o => o.Interview).IsModified = true;
                db.Entry(application).Property(o => o.Comments).IsModified = true;
                db.Entry(application).Property(o => o.Notes).IsModified = true;
                db.Entry(application).Property(o => o.Unfit).IsModified = true;
                db.Entry(application).Property(o => o.Candidate).IsModified = true;
                db.SaveChanges();
                return RedirectToAction("Index");
            //return View(application);
        }

如果你想改变某些值,根据你上面的代码不知道属性被修改了。您可以将属性设置为要更改属性值的 isModified=true

编辑:

            db.Application.Attach(application);
            db.Entry(application).Property(o => o.Interview).IsModified = true;
            db.Entry(application).Property(o => o.Comments).IsModified = true;
            db.SaveChanges();
            return RedirectToAction("Index");

或者您可以使用列表进行以下操作;

var included= new[] { "Interview ", "Comments " };
var entry = context.Entry(obj);
entry.State = EntityState.Modified;

foreach (var name in included)
{
   entry.Property(name).IsModified = true;
}

我想出了解决方法,同时保持 @Html.BeginForm() 为空

编辑动作

// POST: Applications/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Application application, string id)  
        {
            var data = db.Applications.Find(id);
            data.Interview = application.Interview;
            data.Comments = application.Comments;
            data.Notes = application.Notes;
            data.Unfit = application.Unfit;
            data.Candidate = application.Candidate;
            db.SaveChanges();
            return RedirectToAction("Index");
        }