title: ASP.net MVC 3 Edit/Delete issue
body: <p>So I just finished working the project at <a href="http://msdn.microsoft.com/en-us/data/gg685489" rel="nofollow">http://msdn.microsoft.com/en-us/data/gg685489</a>. I am trying to create a CRUD for use with an external database that already exists and I am just using the project as a guide for my own work. I have been able to change certain variable names and connections to connect with the database that I am trying to work with and I have even displayed the data I want using the index method. I have also been able to get the create method working and I am able to create new data in the DB I am working with. Unfortunatlty I have not been able to pull data in when using the Edit or Delete methods or save/delete after I have made the changes I want.</p>
  
  <p>For example when I press the edit button it should pull the record that I clicked on so that I could edit a particular records information but instead it threw an error saying that this variable cannot accept a null value which is why I changed the int to a string in the edit delete methods. <b> This part is solved </b> </p>
  
  <p>I think the issue has something to do with the Edit and Delete methods not pulling the recordset when I tell it to. Does anyone have any idea why it's not pulling the record sets when I edit/delete or saving my changes after I tell it to save? </p>
  
  <p>I have posted my PaController class which has my CRUD methods in it for diagnosis as well as my iamp_mapping class file which has the 3 fields I need to work with. I am 90% sure I am doing something wrong with the controller, but if you need anymore code/information about the issue please leave me a note I will be checking back A LOT because I am soo stuck! Thanks so much for your help!</p>
  
  <h2>PaController</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using DBFirstMVC.Models;
  using System.Data;
  
  namespace DBFirstMVC.Controllers
  {
      public class PaController : Controller
      {
          PaEntities db = new PaEntities();
          //
          // GET: /Pa/
  
          public ActionResult Index()
          {
              using (var db = new PaEntities())
              {
                  return View(db.iamp_mapping.ToList());
              }
          }
  
          //
          // GET: /Pa/Details/5
  
          public ActionResult Details(int id)
          {
              return View();
          }
  
          //
          // GET: /Pa/Create
  
          public ActionResult Create()
          {
              return View();
          } 
  
          //
          // POST: /Pa/Create
  
          [HttpPost]
          public ActionResult Create(iamp_mapping IAMP)
          {
              try
              {
                  using (var db = new PaEntities())
                  {
                      db.iamp_mapping.Add(IAMP);
                      db.SaveChanges();
                  }
  
                  return RedirectToAction("Index");
              }
              catch
              {
                  return View();
              }
          }
  
          //
          // GET: /Pa/Edit/5
  
          public ActionResult Edit(string id)
          {
              using (var db = new PaEntities())
              {
                  return View(db.iamp_mapping.Find(id));
              }
          }
  
          //
          // POST: /Pa/Edit/5
  
          [HttpPost]
          public ActionResult Edit(string id, iamp_mapping IAMP)
          {
              try
              {
                  using (var db = new PaEntities())
                  {
                      db.Entry(IAMP).State = EntityState.Modified;
                      db.SaveChanges();
                      return RedirectToAction("Index");
                  }
              }
              catch
              {
                  return View();
              }
          }
  
          //
          // GET: /Pa/Delete/5
  
          public ActionResult Delete(string id)
          {
              using (var db = new PaEntities())
              {
  
                  return View(db.iamp_mapping.Find(id));
              }
          }
  
          //
          // POST: /Pa/Delete/5
  
          [HttpPost]
          public ActionResult Delete(string id, iamp_mapping IAMP)
          {
              try
              {
                  using (var db = new PaEntities())
                  {
                      db.Entry(IAMP).State = EntityState.Deleted;
                      db.SaveChanges();
                      return RedirectToAction("Index");
                  }
  
              }
              catch
              {
                  return View();
              }
          }
      }
  }
  </code></pre>
  
  <h2>iamp_mapping</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace DBFirstMVC.Models
  {
      public partial class iamp_mapping
      {
          public string PA { get; set; }
          public string MAJOR_PROGRAM { get; set; }
          public string INVESTMENT_AREA { get; set; }
      }
  
  }
  </code></pre>
  
  <h2>Edit View Code</h2>
  
  <pre><code>@model DBFirstMVC.Models.iamp_mapping
  
  @{
      ViewBag.Title = "Edit";
  }
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;iamp_mapping&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.PA)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.PA)
              @Html.ValidationMessageFor(model =&gt; model.PA)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.MAJOR_PROGRAM)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.MAJOR_PROGRAM)
              @Html.ValidationMessageFor(model =&gt; model.MAJOR_PROGRAM)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.INVESTMENT_AREA)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.INVESTMENT_AREA)
              @Html.ValidationMessageFor(model =&gt; model.INVESTMENT_AREA)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <h2>Index.cshtml</h2>
  
  <pre><code>@model IEnumerable&lt;DBFirstMVC.Models.iamp_mapping&gt;
  
  @{
      ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;
              PA
          &lt;/th&gt;
          &lt;th&gt;
              MAJOR PROGRAM
          &lt;/th&gt;
          &lt;th&gt;
              INVESTMENT AREA
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.PA)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.MAJOR_PROGRAM)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.INVESTMENT_AREA)
          &lt;/td&gt;
      &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id=item.PA }) |
          @Html.ActionLink("Details", "Details", new { id=item.PA }) |
          @Html.ActionLink("Delete", "Delete", new { id=item.PA })
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>}</p>
  
  <p>The ActionLink is what I had to fix, when I first posted the question it looked like this</p>
  
  <p><code>@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })</code> </p>
  
  <p>I changed it to the actual primary key PA and deleted the comments. This fixed the problem of data not filling in when trying to edit a row! </p>
  
  <pre><code>    &lt;td&gt;
          @Html.ActionLink("Edit", "Edit", new { id=item.PA }) |
          @Html.ActionLink("Details", "Details", new { id=item.PA }) |
          @Html.ActionLink("Delete", "Delete", new { id=item.PA })
      &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>}</p>
  
  <p></p>
  
  <h2>Delete.cshtml</h2>
  
  <pre><code>@model DBFirstMVC.Models.iamp_mapping
  
  @{
      ViewBag.Title = "Delete";
  }
  
  &lt;h2&gt;Delete&lt;/h2&gt;
  
  &lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;iamp_mapping&lt;/legend&gt;
  
      &lt;div class="display-label"&gt;PA&lt;/div&gt;
      &lt;div class="display-field"&gt;
          @Html.DisplayFor(model =&gt; model.PA)
      &lt;/div&gt;
  
      &lt;div class="display-label"&gt;MAJOR_PROGRAM&lt;/div&gt;
      &lt;div class="display-field"&gt;
          @Html.DisplayFor(model =&gt; model.MAJOR_PROGRAM)
      &lt;/div&gt;
  
      &lt;div class="display-label"&gt;INVESTMENT_AREA&lt;/div&gt;
      &lt;div class="display-field"&gt;
          @Html.DisplayFor(model =&gt; model.INVESTMENT_AREA)
      &lt;/div&gt;
  &lt;/fieldset&gt;
  @using (Html.BeginForm()) {
      &lt;p&gt;
          &lt;input type="submit" value="Delete" /&gt; |
          @Html.ActionLink("Back to List", "Index")
      &lt;/p&gt;
  }
  </code></pre>
  
  <h2>Global.asax.cs</h2>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Routing;
  
  namespace DBFirstMVC
  {
      // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
      // visit http://go.microsoft.com/?LinkId=9394801
  
      public class MvcApplication : System.Web.HttpApplication
      {
          public static void RegisterGlobalFilters(GlobalFilterCollection filters)
          {
              filters.Add(new HandleErrorAttribute());
          }
  
          public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
              routes.MapRoute(
                  "Default", // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                  new { controller = "Pa", action = "Index", id = UrlParameter.Optional } // Parameter defaults
              );
  
          }
  
          protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
  
              RegisterGlobalFilters(GlobalFilters.Filters);
              RegisterRoutes(RouteTable.Routes);
          }
      }
  }
  </code></pre>