title: ASP.NET MVC Edit function not working
body: <p>So I'm creating a basic MVC ASP.NET app that creates friends, displays them and allows you to edit the friends information.</p>
  
  <p>The model is a Friend class with each friend having their own name, id, place, and position.</p>
  
  <pre><code>public class FriendModel
      {
      [Required]
      public int id { set; get; }
      [Required]
      public String mesto { set; get; }
      [Required]
      public String name { set; get; }
      public int pos { set; get; }
  }
  </code></pre>
  
  <p>Whenever I add a new friend it in a List inside the FriendList object which is created once as a static when the controller is launched:</p>
  
  <pre><code>public class FriendList
  {
      private List&lt;FriendModel&gt; lista;
      private int count;
  
      public FriendList()
      {
          lista = new List&lt;FriendModel&gt;();
          count = 0;
      }
  
      public FriendModel getEl(int i)
      {
          return lista.ElementAt(i);
      }
  
      public void add(FriendModel m)
      {
          m.pos = count;
          count++;
          lista.Add(m);
      }
  
      public List&lt;FriendModel&gt; getList()
      {
          return lista;
      }
  }
  </code></pre>
  
  <p>Inside the controller the HttpGet and HttpPost actions look like this:</p>
  
  <pre><code>[HttpGet]
      public ActionResult Edit(int id)
      {
          FriendModel fm = lista.getEl(id);
          return View(fm);
      }
  
      [HttpPost]
      public ActionResult Edit(FriendModel fm)
      {
          FriendModel model = lista.getEl(fm.pos);
          model.id = fm.id;
          model.name = fm.name;
          model.mesto = fm.mesto;
          return Redirect("/Friend/ShowAll");
      }
  </code></pre>
  
  <p>And the edit view look like this:</p>
  
  <pre><code>    @model Lab1.Models.FriendModel
  
  
      @{
          ViewBag.Title = "Edit";
      }
  
      &lt;h2&gt;Edit&lt;/h2&gt;
  
      @using (Html.BeginForm())
      {
          &lt;label&gt;Name&lt;/label&gt;&lt;br /&gt;
          &lt;div&gt;@Html.TextBoxFor(m =&gt; m.name)&lt;/div&gt;
  
          &lt;label&gt;Id&lt;/label&gt;&lt;br /&gt;
          &lt;div&gt;@Html.TextBoxFor(m =&gt; m.id)&lt;/div&gt;
  
          &lt;label&gt;Place&lt;/label&gt;&lt;br /&gt;
          &lt;div&gt;@Html.TextBoxFor(m =&gt; m.mesto)&lt;/div&gt;
          &lt;br/&gt;
          &lt;label&gt;@Model.pos&lt;/label&gt;
          &lt;input id="Submit1" type="submit" value="Edit" /&gt;
      }
  </code></pre>
  
  <p>The problem is that whenever I edit something it doesn't make changed to the selected item I want to edit but always the first item in the list is being edited with the information I've edited in the selected item while the selected item is not being edited.</p>
  
  <p>I suspect the problem comes in this area here in the controller:</p>
  
  <pre><code>[HttpPost]
      public ActionResult Edit(FriendModel fm)
      {
          FriendModel model = lista.getEl(fm.pos);
          model.id = fm.id;
          model.name = fm.name;
          model.mesto = fm.mesto;
          return Redirect("/Friend/ShowAll");
      }
  </code></pre>
  
  <p>Specifically in this line of code:</p>
  
  <pre><code>FriendModel model = lista.getEl(fm.pos);
  </code></pre>
  
  <p>It seems to be getting the object at the 0 position of the list instead of the selected position.</p>
  
  <p>Any ideas?</p>