views:

17

answers:

1

Hi,

I am using MVC 2 and EF 4.

I am trying to update my Application entity using my own stored procedure, but it is not updating. I checked out SQL Profiler and it is not even reaching the database. I also use an insert stored procedure and it works fine. Both stored procedures (insert and update) are mapped correctly. Does anyone know what I am doing wrong?

Here is my EditApplication action event:

[HttpPost]
public ActionResult EditApplication(int id, ApplicationViewModel applicationViewModel)
{
   if (ModelState.IsValid)
   {
      try
      {
         Mapper.CreateMap<ApplicationViewModel, Application>();
         var application = (Application)Mapper.Map(applicationViewModel, typeof(ApplicationViewModel), typeof(Application));
         application.ApplicationID = id;

         UpdateModel(application);

         // Edit application
         applicationRepository.UpdateApplication(application);

         // If success, redirect to confirmation page
         return RedirectToAction("Confirmation", new { id = application.ApplicationID });
      }
      catch (Exception ex)
      {
         // TODO
      }
   }

   applicationViewModel.AccountTypes = accountTypeRepository.GetAccountTypes();
   return View("CreateApplication", applicationViewModel);
}

Here is my repository Update method:

private MyEntities db = new MyEntities();

public void UpdateApplication(Application application)
{
   db.SaveChanges();
}

Thanks Brendan

A: 

This my changes, not sure if it is correct but it is working.

Action event:

[HttpPost]
public ActionResult EditApplication(int id, ApplicationViewModel applicationViewModel)
{
   var application = applicationRepository.GetApplicationByID(id);

   if (ModelState.IsValid)
   {
      try
      {
         Mapper.CreateMap<ApplicationViewModel, Application>();
         application = (Application)Mapper.Map(applicationViewModel, typeof(ApplicationViewModel), typeof(Application));

         application.ApplicationStateID = 1;
         application.ApplicationID = id;

         // Edit application
         applicationRepository.UpdateApplication(application);

         // If success, redirect to confirmation page
         return RedirectToAction("Confirmation", new { id = application.ApplicationID });
      }
      catch (Exception ex)
      {
         // TODO
      }
   }

   applicationViewModel.AccountTypes = accountTypeRepository.GetAccountTypes();
   return View("CreateApplication", applicationViewModel);
}

The update method in my repository:

private MyEntities context = new MyEntities();

public void UpdateApplication(Application application)
{
   context.Applications.ApplyCurrentValues(application);
   context.SaveChanges();
}
Brendan Vogt