简体   繁体   中英

TryUpdateModel and System.MissingMethodException

In my Action, I have an error of type System.MissingMethodException when I use TryUpdateModel. I use this in several place in my Controller without issue, so it mean a problem on my model ?

In this case, I use a derived class from my domain.

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 is generated. I derive from this class to add Attributes and I use this next in my Model.

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

To show, I use this Action

    public ActionResult AfficheDetailTypeOperation(Int64 idTypeOperation)
    {
        DetailTypeOperationModel d = new DetailTypeOperationModel
        {
            IdTypeOperation = idTypeOperation,
            TypeOperationDisplay = _srvTypeOperation.Charger(idTypeOperation).ToDisplay()
        };

        return View("GererTypeOperation", d);
    }

To retrieve datas sent

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

        _srvTypeOperation.Modifier(d.TypeOperationDisplay);

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

And it's on this Action, that I have issue on TryUpdateModel. With step by step debug, I can't see why this component catch an error and where is this missing method ?

Thanks for helping :)

Make your TypeOperationDisplay property virtual in your DetailTypeOperationModel class.

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

I am guessing here, but my theory is that EF is trying to create a proxy of DetailTypeOperationModel, and can't because your own class property is not virtual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM