ApplicationDbContext.Update()不会更新,但会将其另存为新记录

时间:2016-05-15 17:38:16

标签: asp.net-core-mvc entity-framework-core

我正在使用新的APS.NET 5 RC1环境,但默认的编辑操作会创建对象的新实体。我一整天都在搜索它出错的地方,但我无法找出它出错的原因。

控制器:

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using SSTv5.Models;
using SSTv5.Models.Organisations;
using SSTv5.Models.Views;
using System.Collections.Generic;
using SSTv5.Models.Global;

namespace SSTv5.Controllers
{

public class OrganisationTypesController : Controller
{
    private ApplicationDbContext _context;
    private List<Breadcrumb> _bcList = new List<Breadcrumb>();

    public OrganisationTypesController(ApplicationDbContext context)
    {
        _context = context;    
    }

    #region Edit
    // GET: OrganisationTypes/Edit/5
    public async Task<IActionResult> Edit(int? id)
    {

        _bcList.Add(new Breadcrumb("OrganisationTypes", true, "Index", "Index"));
        _bcList.Add(new Breadcrumb("Edit", false));
        ViewBag.BcList = _bcList;

        if (id == null)
        {
            return HttpNotFound();
        }

        OrganisationType organisationType = await _context.OrganisationType.SingleAsync(m => m.OrganisationTypeId == id);
        if (organisationType == null)
        {
            return HttpNotFound();
        }
        return View(organisationType);
    }

    // POST: OrganisationTypes/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(OrganisationType organisationType)
    {
        if (ModelState.IsValid)
        {
            _context.Update(organisationType);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        return View(organisationType);
    }
    #endregion
}
}

形式:

@model SSTv5.Models.Organisations.OrganisationType

@{
    ViewData["Title"] = "Edit";
}

<h2>Edit</h2>

<form asp-action="Edit">
<div class="form-horizontal">
    <h4>OrganisationType</h4>
    <hr />
    <div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="OrganisationTypeName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="OrganisationTypeName" class="form-control" />
            <span asp-validation-for="OrganisationTypeName" class="text-danger" />
        </div>
    </div>

    <div class="form-group">
        <label asp-for="ClassIcon" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="ClassIcon" id="classIcon" />
            <span asp-validation-for="ClassIcon" class="text-danger" />
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Edit" class="btn btn-default" />
        </div>
    </div>
</div>
</form>

<div>
<a asp-action="Index">Back to List</a>
</div>

@section Scripts {
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}

1 个答案:

答案 0 :(得分:1)

对于有这个问题的任何人来说,答案很简单。 表单不会发送对象的ID。您需要做的唯一事情是将id放在表单中的隐藏字段中。然后它会正常工作。