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:
2
  • TryUpdateModel() would swallow exception on failure.. Commented Apr 18, 2011 at 12:38
  • What does your HTML look like? Commented Apr 18, 2011 at 12:52

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
You need to share your model as well. Also put the full error stack.
Inner exception is null. 1 min @ model.
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".
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."
|
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

1 Comment

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
0

You need to submit the changes on the ClientService

1 Comment

Hi, service.Update(c) updates model. Problem is that value in c didn't get set prior to calling this method. Thank you
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.