在我的 CRUD 屏幕中编辑项目时,我的 ID 自动设置为 0

When editing a item in my CRUD screen, my Id sets automatically to 0

我正在研究 ASP.NET 核心应用程序中的 CRUD 功能。我遇到这个问题,当我尝试编辑一个字符(字符在荷兰语中是 Karakter)时,我提供的字符的 ID 总是重置为 0。这是一个问题,因为我现在总是编辑Id = 0 的角色,我无法编辑其他 id 的角色。

我的看法:

@model IEnumerable<Apex.Models.KarakterViewModel>

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

<h1>Karakter</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.KarakterId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.KarakterSoort)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.KarakterNaam)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.KarakterId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.KarakterSoort)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.KarakterNaam)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new {KarakterId = item.KarakterId}) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Home", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
}
    </tbody>
</table>

我的编辑视图:

@model Apex.Models.KarakterViewModel

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

<h1>Edit</h1>

<h4>KarakterViewModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="KarakterId" class="control-label"></label>
                <input asp-for="KarakterId" class="form-control" />
                <span asp-validation-for="KarakterId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="KarakterSoort" class="control-label"></label>
                <input asp-for="KarakterSoort" class="form-control" />
                <span asp-validation-for="KarakterSoort" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="KarakterNaam" class="control-label"></label>
                <input asp-for="KarakterNaam" class="form-control" />
                <span asp-validation-for="KarakterNaam" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

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

我的控制器:

public class KarakterController : Controller
{
    private readonly IKarakterLogic _karakterLogic;

    public KarakterController()
    {
        // Waarom moet  apexfactory ervoor?
        _karakterLogic = ApexFactory.ApexFactory.GetKarakterLogic();
    }

    public ActionResult Index()
    {
        return Karakter();
    }

    // GET: Karakter
    public ActionResult Karakter()
    {
        var allKarakters = _karakterLogic.GetAllKarakters();
        var karakters = new List<KarakterViewModel>();

        foreach (var karakter in allKarakters)
        {
            karakters.Add(new KarakterViewModel
            {
                KarakterId = karakter.KarakterId,
                KarakterNaam = karakter.KarakterNaam,
                KarakterSoort = karakter.KarakterSoort
            });
        }

        return View(karakters);
    }

    public ActionResult Delete(int KarakterId)
    {
        _karakterLogic.DeleteKarakter(KarakterId);
        return RedirectToAction("Karakter");
    }

    [HttpGet]
    public ActionResult Create()
    {
        var karakterViewModel = new KarakterViewModel();
        return View(karakterViewModel);
    }

    [HttpPost]
    public ActionResult Create(KarakterViewModel karakter)
    {
        _karakterLogic.CreateKarakter(karakter);
        return RedirectToAction("Karakter");
    }

    [HttpGet]
    public ActionResult Edit()
    {
        KarakterViewModel karakterViewModel = new KarakterViewModel();
        _karakterLogic.GetById(karakterViewModel);
        return View(karakterViewModel);
    }

    [HttpPost]
    public ActionResult Edit(KarakterViewModel karakterViewModel)
    {
        _karakterLogic.UpdateKarakter(karakterViewModel);
        return RedirectToAction("Karakter");
    }

    public ActionResult AllKarakters()
    {
        return Karakter();
    }
}

我的逻辑层:

public class KarakterLogic : IKarakterLogic
{
        private IKarakterContext karakterContext { get;}

        public KarakterLogic(IKarakterContext context)
        {
            karakterContext = context;
        }

        public void CreateKarakter(IKarakter _karakter)
        {
            var karakter = new KarakterModel
            {
                KarakterNaam = _karakter.KarakterNaam,
                KarakterSoort = _karakter.KarakterSoort
            };

            karakterContext.CreateKarakter(karakter);
        }

        public IEnumerable<IKarakter> GetAllKarakters()
        {
            return karakterContext.GetKarakters();
        }

        public IKarakter UpdateKarakter(IKarakter karakter)
        {
            karakterContext.UpdateKarakter(karakter);
            return karakter;
        }

        public void DeleteKarakter(int KarakterId)
        {
            karakterContext.DeleteKarakter(KarakterId);
        }

        public IKarakter GetById(IKarakter karkter)
        {
            return karakterContext.GetById(karkter);
        }    
}

我认为我的逻辑和 DAL 工作正常,因为它确实以正确的方式编辑 ID 为 0 的角色。问题是我不只是想编辑 id = 0.

的角色

我如何才能编辑具有请求 ID 的角色(而不是 ID 为 0 的角色)?

注意:当我单击所需的字符(在本例中为第二个字符)时,我的 HTTP link 是这样的:

所以它确实识别出我要编辑的角色的Id是2。 谁能帮帮我?

您的代码

@Html.ActionLink("Edit", "Edit", new {Id= item.KarakterId})

KarakterId作为参数传递给Edit方法,所以你应该在Edit方法中接收它,像这样:

[HttpGet]      
public ActionResult Edit(int Id)
{
 //Get the corresponding ViewModel according to this Id, and then return to the view.
 var allKarakters = _karakterLogic.GetAllKarakters();
    var karakters = new List<KarakterViewModel>();
    foreach (var karakter in allKarakters)
    {
        karakters.Add(new KarakterViewModel
        {
            KarakterId = karakter.KarakterId,
            KarakterNaam = karakter.KarakterNaam,
            KarakterSoort = karakter.KarakterSoort
        });
    }
  var viewmodel=karakters.Where(c=>c.KarakterId==Id).FirstOrDefault();
  return View(viewmodel);
}

这只是一个例子,你可以在你的逻辑中根据Id写一个方法来获取模型,然后使用它。