NET Core中如何修复不起作用的ActionResult更新?

时间:2019-05-26 17:47:49

标签: asp.net-core

我正在学习.NET Core,并且正在使用this tutorial中的代码。但是我的更新sql无法正常工作。

这是索引视图代码:

        public ActionResult Index(int? id)
    {
        ViewBag.Operation = id;
        ViewBag.Name = db.Chars.ToList();
        Chars Chars = db.Chars.Find(id);
        return View(Chars);
    }

就目前而言,它可以从sql中看到结果,这是更新的部分:

        public ActionResult Update(Chars Chars)
    {
        if (ModelState.IsValid)
        {
            db.Entry(Chars).State = EntityState.Modified;
            db.SaveChanges();
        }
        return RedirectToAction("Index", new { id = 0 });
    }

这是index.cshtml部分:

   @using (Html.BeginForm()
{
    @foreach (var item in (IEnumerable<MVC__test_2.Chars>)ViewBag.Name)
    {
        <div class="form-group">
            <div class="col-md-10">
                @Html.EditorFor(modelItem => item.CharName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.HiddenFor(modelItem => item.CharID, new { id = item.CharID })
            </div>
        </div>
        @Html.ActionLink("Index", "Index", new { id = item.CharID }) 
<input type="submit" value="Update" name="Update"
       style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0)   ? "display:block" : "display:none") />
    }
}

1 个答案:

答案 0 :(得分:0)

根据您提供的教程,我进行了演示以进行测试,并很好地更新了数据。以下是工作示例,您可以根据需要参考并进行修改。

模型

public class Description
{
    public int Id { get; set; }
    public string Display { get; set; }
}

控制器

public IActionResult Index(int? id)
    {
        ViewBag.Operation = id;
        ViewBag.Name = _context.Description.ToList();
        Description description= _context.Description.Find(id);
        return View(description);
    }

public ActionResult Update(Description description)
    {
        if (ModelState.IsValid)
        {
            _context.Entry(description).State = EntityState.Modified;
            _context.SaveChanges();
        }
        return RedirectToAction("Index", new { id = 0 });
    }

Index.cshtml ,您应该在修改部分中隐藏已修改数据的ID。

@model WebApplication1.Models.Description 

@using (Html.BeginForm("Update", "Home", FormMethod.Post))
{
    @foreach (var item in (IEnumerable<WebApplication1.Models.Description >)ViewBag.Name)
    {
        <div class="form-group">
           <div class="col-md-10">
               @Html.EditorFor(modelItem => item.Display, new { htmlAttributes = new { @class = "form-control" } })
               @Html.HiddenFor(modelItem => item.Id, new { id = item.Id })
           </div>
       </div> 
       @Html.ActionLink("Edit", "Index", new { id = item.Id })
   }

   // Create or Update data
   @Html.AntiForgeryToken()
   <div class="form-horizontal">
       @Html.ValidationSummary(true)
       <fieldset>
          <legend> <b>Entry Screen</b></legend>
          <div class="form-group">
             @Html.LabelFor(model => model.Display, new { @class = "control-label col-md-2" })
             @Html.HiddenFor(model => model.Id)
             <div class="col-md-10">
                @Html.EditorFor(model => model.Display)
                @Html.ValidationMessageFor(model => model.Display)
             </div>
          </div>

          <div class="form-group">
              <p>
                  <input type="submit" value="Create" name="Create"
                       style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0) ? "display:none" : "display:block") />
                  <input type="submit" value="Update" name="Update"
                       style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0) ? "display:block" : "display:none") />
              </p>
          </div>
       </fieldset>
   </div>
}