title: Implementing CRUD in MVC
body: <p>I'm still new to MVC. now in my controller I want to perform basic CRUD such as Create, Edit, Details. </p>
  
  <p>I use MySql as the database.</p>
  
  <p>When I run the program in debug mode, it shows no error. But, in create page, when I click "submit" button, the new data entered is not updated in the index. I was wondering why and need help here. </p>
  
  <p>Could you guys tell me where in create and edit I do wrong? Thanks in advance.</p>
  
  <p>controller:</p>
  
  <pre><code>    //GET UserActivity/Details/5
      public ActionResult Details(int id)
      {
          string sqlQuery = @"
            SELECT ua.Id
              ,ua.CreatedBy
              ,ua.CreatedOn
              ,ua.ModifiedBy
              ,ua.ModifiedOn
              ,ua.ContactId
              ,ua.EntityName
              ,ua.EntityId
              ,ua.StatusCode
              ,ua.StateCode
              ,ua.ActivityType
              ,ua.ActivityStatus
              ,ua.DueDate
              ,ua.ActualEndDate
              ,ua.MasqueradeOn
              ,ua.MasqueradeBy 
  
              FROM UserActivity ua
              INNER JOIN Contact c ON c.Id = ua.ContactId
              WHERE ua.id = @UserActivityId
          ";
  
          UserActivityModels userActivity = null;
  
          using (IDbConnection db = new MySqlConnection
              (ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].
              ConnectionString))
          {
              userActivity = (UserActivityModels)db.Query&lt;UserActivityModels&gt;(sqlQuery
                 , new
                 {
                     @UserActivityId = id
                 }).FirstOrDefault();
  
          }
          return View(userActivity);
      }
  
      /* // POST: /UserActivity/Details
      [HttpPost]
      public ActionResult Details(FormCollection collection)
      {
          try
          {
              // TODO: Add insert logic here
  
              return RedirectToAction("Details");
          }
          catch
          {
              return View();
          }
      }
      */
  
  
      //----------------------------------------------------------------------------------------------------//
  
  
      //GET /UserActivity/Create
      public ActionResult Create()
      {
  
          return View();
      }
  
  
      //
      // POST: /UserActivity/Create
      [HttpPost]
      public ActionResult Create(FormCollection collection)
      {
          try
          {
              /*
              if (ModelState.IsValid)
              {
                  userActivity.Add(useractivity);
                  */
              UserActivityModels useractivity = new UserActivityModels();
  
              int Id = Convert.ToInt32(collection["Id"]);
              int CreatedBy = Convert.ToInt32(collection["Created By"]);
              DateTime CreatedOn = Convert.ToDateTime(collection["Created On"]);
              int ModifiedBy = Convert.ToInt32(collection["Modified By"]);
              DateTime ModifiedOn = Convert.ToDateTime(collection["Modified On"]);
              string ContactId = collection["Contact Id"];
              int StatusCode = Convert.ToInt32(collection["Status Code"]);
              int StateCode = Convert.ToInt32(collection["State Code"]);
              string EntityName = collection["Entity Name"];
              int EntityId = Convert.ToInt32(collection["Entity Id"]);
              DateTime DueDate = Convert.ToDateTime(collection["Due Date"]);
              DateTime ActualEndDate = Convert.ToDateTime(collection["Actual EndDate"]);
              DateTime MasqueradeOn = Convert.ToDateTime(collection["Masquerade On"]);
              DateTime MasqueradeBy = Convert.ToDateTime(collection["Masquerade By"]);
              string ContactName = collection["Contact Name"];
  
              userActivity.Add(useractivity);
  
  
              return RedirectToAction("Index");
  
          }
  
          catch
          {
              return View();
          }
  
      }
  
  
  
  
      //---------------------------------------------------------------------//
  
  
  
      // GET: /UserActivity/Edit/5
      public ActionResult Edit(int id)
      {
          //ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString);
          string query = @"
            SELECT ua.Id
              ,ua.CreatedBy
              ,ua.CreatedOn
              ,ua.ModifiedBy
              ,ua.ModifiedOn
              ,ua.ContactId
              ,ua.EntityName
              ,ua.EntityId
              ,ua.StatusCode
              ,ua.StateCode
              ,ua.ActivityType
              ,ua.ActivityStatus
              ,ua.DueDate
              ,ua.ActualEndDate
              ,ua.MasqueradeOn
              ,ua.MasqueradeBy 
      ,concat(c.FirstName, ' ', c.LastName) AS ContactName
              FROM UserActivity ua
              INNER JOIN Contact c ON c.Id = ua.ContactId
              WHERE ua.id = @userActivityId
  
          ";
  
          UserActivityModels userActivity = null;
  
          using (IDbConnection db = new MySqlConnection(ConfigurationManager.
              ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString))
          {
              // userActivity = (UserActivityModels)db.Query&lt;UserActivityModels&gt;(query);
  
  
              userActivity = db.Query&lt;UserActivityModels&gt;(query
                  , new
                  {
                      @UserActivityId = id
                  }).FirstOrDefault();
          }
          return View(userActivity);
      }
  
      // POST: /UserActivity/Edit/
      [HttpPost]
      public ActionResult Edit()
      {
          if (ModelState.IsValid)
          {
  
              // TODO: Add update logic here
  
              return RedirectToAction("Index");
          }
          return View(userActivity);
  
      }
  
  
      //------------------------------------------------------------------------//
  
  
      // GET: /UserActivity/Delete/
      public ActionResult Delete(int? id)
      {
          return View();
      }
  
      // POST: /SavedCar/Delete/5
      [HttpPost]
      public ActionResult Delete(int id, FormCollection collection)
      {
  
  
          try
          {
              // TODO: Add delete logic here
  
              return RedirectToAction("Index");
          }
          catch
          {
              return View();
          }
      }
  }
  </code></pre>
  
  <p>userActivity Model:</p>
  
  <pre><code> public class UserActivityModels
  {
      [Display(Name = "Id")]
      public int Id { get; set; }
  
      [Display(Name = "Created By")]
      public int CreatedBy { get; set; }
  
      [Display(Name = "Created On")]
      [DataType(DataType.DateTime)]
      public DateTime CreatedOn { get; set; }
  
      [Display(Name = "Modified By")]
      public int ModifiedBy { get; set; }
  
      [Display(Name = "Modified On")]
      [DataType(DataType.DateTime)]   
      public DateTime ModifiedOn { get; set; }
  
      [Display(Name = "Contact Id")]
      public string ContactId { get; set; }
  
      [Display(Name = "Entity Name")]
      public string EntityName { get; set; }
  
      [Display(Name = "Entity Id")]
      public int EntityId { get; set; }
  
      [Display(Name = "Status Code")]
      public int StatusCode { get; set; }
  
      [Display(Name = "State Code")]
      public int StateCode { get; set; }
  
      [Display(Name = "Activity Type")]
      public int ActivityType { get; set; }
  
      [Display(Name = "Activity Status")]
      public string ActivityStatus { get; set; }
  
      [Display(Name = "DueDate")]
      [DataType(DataType.DateTime)]
      public DateTime DueDate { get; set; }
  
      [Display(Name = "Actual EndDate")]
      [DataType(DataType.DateTime)]
      public DateTime ActualEndDate { get; set; }
  
      [Display(Name = "Masquerade On")]
      [DataType(DataType.DateTime)]
      public DateTime MasqueradeOn { get; set; }
  
      [Display(Name = "Masquerade By")]
      public int MasqueradeBy { get; set; }
  
      [Display(Name = "Contact Name")]
      public string ContactName { get; set; }
  </code></pre>