MVC2抛出InvalidOperationException异常中的UpdateModel(),试图更新id字段 [英] MVC2 throws InvalidOperationException in UpdateModel(), trying to update the id field

查看:214
本文介绍了MVC2抛出InvalidOperationException异常中的UpdateModel(),试图更新id字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC2应用今天的悲伤给我...我想编辑数据库记录,使用下面的控制器code:

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);
        }
    }

我得到了高层的例外式的模式Model.Entity无法更新,当我深入到ModelState中我看到试图更新标识字段时,它的失败 - 设置id属性仅使用.NET 3.5+支持实体反序列化过程。

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".

现在的问题是,我怎么能告诉的UpdateModel()不更新Id字段?我不希望它来更新领域!

The 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

推荐答案

尝试

UpdateModel(tester, formValues.ToValueProvider());

并确保编号不包含在formValues​​。

and make sure Id is not included in the formValues.

这篇关于MVC2抛出InvalidOperationException异常中的UpdateModel(),试图更新id字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆