2

I'd like to update a Client type entity.

[HttpPost]
public ActionResult Manage(String id, FormCollection collection)
{
    // Create service
    ClientService service = new ClientService();

    // Read existing client
    Client c = service.FindByUsername(id);

    // Update client by using values from submitted form collection
    UpdateModel(c, "Client");
    service.Update(c);

    return View(c);            
}

Service returns Client type entity. Client has the following properties: Username, FirstName, LastName, Id - these are the keys in submitted collection.

Additinonally, client entity has a list of orders (added by SQL Metal) as well as a Version field for object tracking.

When the UpdateModel line gets hit, it doesn't error, but the values in object c don't get updated. The problem isn't in service.Update(c), but in UpdateModel(c, "Client").

What am I doing wrong?

Thank you

Edit: Client is mapped by SQL metal.

Its attributes are as follows:

  1. int Id
  2. String Username;
  3. String Firstname;
  4. String Lastname;
  5. Timestamp Version
  6. IQuerable Orders;

Error (Inner exception is null)

System.InvalidOperationException was unhandled by user code
  Message=The model of type 'Shop.MVC.Data.Client' could not be updated.
  Source=System.Web.Mvc
  StackTrace:
       at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
       at System.Web.Mvc.Controller.UpdateModel[TModel](TModel model)
       at Shop.MVC.Web.Controllers.ClientController.Manage(String id, FormCollection collection) in C:\Codebox\ARE002\VideoPlayerPrototype\Shop.MVC.Web\Controllers\ClientController.cs:line 45
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException:
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404

4 Answers4

3

The most likely problem is none of the properties start with "Client".

Without knowing the detail of your model, it is difficult to say but remove the "Client" and I believe that should fix the problem.


UPDATE

You are likely to have some validation rules. Try using TryUpdateModel() which does not do validation on the model.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • I get the "The model of type 'Shop.MVC.Data.Client' could not be updated." exception. I can't debug this as it gets skipped by Visual Studio. I have tried this before. –  Apr 18 '11 at 12:35
  • You need to share your model as well. Also put the full error stack. – Aliostad Apr 18 '11 at 12:36
  • Inner exception is null. 1 min @ model. –  Apr 18 '11 at 12:37
  • I need to see some code for the class. But I can see from the property names that it will not be updated at all as none starts with "Client". – Aliostad Apr 18 '11 at 12:51
  • I have removed the client and I get the exception which I wrote in the first comment: "The model of type 'Shop.MVC.Data.Client' could not be updated." –  Apr 18 '11 at 12:59
  • What is the detail of the exception?? I need full stack trace of the error. – Aliostad Apr 18 '11 at 13:02
  • TryUpdateModel method doesn't work. It ignores validation exception which is thrown by the UpdateModel method –  Apr 18 '11 at 13:25
1

I would agree with Sergey that you need to call Save changes in order for this to be persisted. From your post action I do not see anywhere where this is being persisted to the DB. You're just calling UpdateModel but there is no SaveChanges that I can see.

Hope this helps

imdondo
  • 132
  • 2
  • 12
  • Hi, thank you for the reply. I have said that "When the UpdateModel line gets hit, it doesn't error, but the values in object c don't get updated. The problem isn't in service.Update(c), but in UpdateModel(c, "Client")." When the debugger goes past service.Update(c) line it should change values in c, which it doesn't. Method to persist an object works and it has been tested. :( Thank you for trying to help –  Apr 18 '11 at 13:48
0

You need to submit the changes on the ClientService

Sergei Golos
  • 4,342
  • 1
  • 18
  • 21
  • Hi, service.Update(c) updates model. Problem is that value in c didn't get set prior to calling this method. Thank you –  Apr 18 '11 at 13:51
0

Problem was in a structure of HTML - there was a nested form which has caused UpdateModel method to fail as it contained FormCollection for both forms.