开发者

MVC2 throws InvalidOperationException in UpdateModel(), trying to update the id field

My MVC2 app is giving me grief today... I want to edit a database record, using the following Controller code:

    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Admin")]
    public virtual ActionResult Edit(int id, FormCollection formValues)
    {
        var masterDataProxy = MasterDataChannelFactory.OpenChannel();
        var tester = masterDataProxy.GetTester(id);
        masterDataProxy.CloseChannel();

        if (null == tester)
        {
            return View(Views.NotFound);
        }

        try
        {
            UpdateModel(tester);

            var adminProxy = AdminChannelFactory.OpenChannel();
            adminProxy.AddUpdateTester(tester);
            adminProxy.CloseChannel();

            return RedirectToAction(Actions.Index());
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("Tester", ex.Message);

            return View(tester);
        }
    }

I'm getting the high-level exception "The model of type 'Model.Entity' could not be updated", and when I drill down into the ModelState I see it's failing when trying to update the Id field -- "Setting the Id property is only supported with .NET 3.5+ during entity deserialization".

The开发者_如何学JAVA question is, how can I tell UpdateModel() not to update the Id field? I don't want it to update that field!!

Any ideas? Dave


Try

UpdateModel(tester, formValues.ToValueProvider());

and make sure Id is not included in the formValues.


Use TryUpdateModel(tester) insted of UpdateModel(tester)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜