TryUpdateModel and System.MissingMethodException

3

在我的操作中,当我使用TryUpdateModel时,出现了类型为System.MissingMethodException的错误。我在控制器中的多个位置使用它而没有问题,这意味着问题出在我的模型上吗?

在这种情况下,我使用了从我的域派生的类。

public class TypeOperationDisplay : TypeOperation
{
    public TypeOperationDisplay(TypeOperation to)
    {
        Id = to.Id;
        Code = to.Code;
        Libelle = to.Libelle;
        LibelleSaisie = to.LibelleSaisie;
    }

    [ScaffoldColumn(false)]
    public override long Id
    {
        get
        {
            return base.Id;
        }
        set
        {
            base.Id = value;
        }
    }

    [HtmlPropertiesAttribute(MaxLength=255, Size=50, ReadOnly=true)]
    [DisplayName("")]
    public override string Code
    {
        get
        {
            return base.Code;
        }
        set
        {
            base.Code = value;
        }
    }
}

TypeOperation被生成。我从这个类派生出来添加属性,然后在我的模型中使用它。

public class DetailTypeOperationModel : ViewModelBase
{
    public Int64 IdTypeOperation { get; set; }
    public TypeOperationDisplay TypeOperationDisplay { get; set; }
}

为了展示,我使用这个动作。
    public ActionResult AfficheDetailTypeOperation(Int64 idTypeOperation)
    {
        DetailTypeOperationModel d = new DetailTypeOperationModel
        {
            IdTypeOperation = idTypeOperation,
            TypeOperationDisplay = _srvTypeOperation.Charger(idTypeOperation).ToDisplay()
        };

        return View("GererTypeOperation", d);
    }

获取发送的数据

    [HttpPost]
    public ActionResult ModifieTypeOperation(Int64 idTypeOperation, FormCollection fc)
    {
        DetailTypeOperationModel d = new DetailTypeOperationModel();
        TryUpdateModel<DetailTypeOperationModel>(d);

        _srvTypeOperation.Modifier(d.TypeOperationDisplay);

        return View("Index", new AdministrationModel());            
    }

在这个Action中,我在TryUpdateModel上遇到了问题。通过逐步调试,我看不出为什么这个组件会捕获错误以及缺少哪个方法?

感谢您的帮助 :)

1个回答

1
在你的DetailTypeOperationModel类中,将TypeOperationDisplay属性设置为虚拟
public class DetailTypeOperationModel : ViewModelBase
{
    public Int64 IdTypeOperation { get; set; }
    public virtual TypeOperationDisplay TypeOperationDisplay { get; set; }
}

我猜测一下,我的理论是EF试图创建DetailTypeOperationModel的代理,但无法创建,因为您自己的类属性不是虚拟的。

我正在使用NHibernate,但是virtual解决了我的问题。谢谢 :) - User.Anonymous

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接