title: When editing a item in my CRUD screen, my Id sets automatically to 0
body: <p>I am working on the CRUD functions in my ASP.NET Core application. I have this problem where when I try to edit a character (character is <code>Karakter</code> in Dutch) the <code>ID</code> of the character that I provide is always reset to 0. This is a problem because I now always edit the character with Id = 0 and I can't edit characters with an other id.</p>
  <p>My view:</p>
  <pre><code>@model IEnumerable&lt;Apex.Models.KarakterViewModel&gt;
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Karakter&quot;;
  }
  
  &lt;h1&gt;Karakter&lt;/h1&gt;
  
  &lt;p&gt;
      &lt;a asp-action=&quot;Create&quot;&gt;Create New&lt;/a&gt;
  &lt;/p&gt;
  &lt;table class=&quot;table&quot;&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.KarakterId)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.KarakterSoort)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.KarakterNaam)
              &lt;/th&gt;
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
  @foreach (var item in Model) {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.KarakterId)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.KarakterSoort)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.KarakterNaam)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.ActionLink(&quot;Edit&quot;, &quot;Edit&quot;, new {KarakterId = item.KarakterId}) |
                  @Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { /* id=item.PrimaryKey */ }) |
                  @Html.ActionLink(&quot;Delete&quot;, &quot;Home&quot;, new { /* id=item.PrimaryKey */ })
              &lt;/td&gt;
          &lt;/tr&gt;
  }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  <p>My edit view:</p>
  <pre><code>@model Apex.Models.KarakterViewModel
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Edit&quot;;
  }
  
  &lt;h1&gt;Edit&lt;/h1&gt;
  
  &lt;h4&gt;KarakterViewModel&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form asp-action=&quot;Edit&quot;&gt;
              &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;KarakterId&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;KarakterId&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;KarakterId&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;KarakterSoort&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;KarakterSoort&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;KarakterSoort&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;KarakterNaam&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;KarakterNaam&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;KarakterNaam&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;btn btn-primary&quot; /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;a asp-action=&quot;Karakter&quot;&gt;Back to List&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  <p>My controller:</p>
  <pre><code>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&lt;KarakterViewModel&gt;();
  
          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(&quot;Karakter&quot;);
      }
  
      [HttpGet]
      public ActionResult Create()
      {
          var karakterViewModel = new KarakterViewModel();
          return View(karakterViewModel);
      }
  
      [HttpPost]
      public ActionResult Create(KarakterViewModel karakter)
      {
          _karakterLogic.CreateKarakter(karakter);
          return RedirectToAction(&quot;Karakter&quot;);
      }
  
      [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(&quot;Karakter&quot;);
      }
  
      public ActionResult AllKarakters()
      {
          return Karakter();
      }
  }
  </code></pre>
  <p>My logic layer:</p>
  <pre><code>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&lt;IKarakter&gt; 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);
          }    
  }
  </code></pre>
  <p>I think my logic and DAL are working fine, because it does edit the character with id 0 in the right way. The problem is that I don't only want to edit the character with id = 0.</p>
  <p>How do I get it that I edit the character with the requested ID (and not the character with id 0.)?</p>
  <p>Note: When I click the desired character (in this case the second character) my HTTP link is this:</p>
  <p><a href="https://i.stack.imgur.com/ALeYM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ALeYM.png" alt="Httplink" /></a></p>
  <p>So there it does recognize that the Id of the character I want to edit is 2.
  Can anyone help me?</p>