c# - TryUpdateModel 和 System.MissingMethodException

标签 c# asp.net-mvc

在我的 Action 中,当我使用 TryUpdateModel 时出现 System.MissingMethodException 类型的错误。 我在我的 Controller 中的几个地方使用它没有问题,所以这意味着我的模型有问题?

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

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

为了展示,我使用了这个Action

    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 的问题。 通过逐步调试,我看不出为什么这个组件会捕获错误,这个缺失的方法在哪里?

感谢您的帮助:)

最佳答案

在 DetailTypeOperationModel 类中使您的 TypeOperationDisplay 属性虚拟

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

我在这里猜测,但我的理论是 EF 正在尝试创建 DetailTypeOperationModel 的代理,但不能,因为您自己的类属性不是虚拟的。

关于c# - TryUpdateModel 和 System.MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6029444/

相关文章:

c# - LINQ查询匹配多个词

c# - 为什么我的 Route.Ignore 不能处理我的 ASP.NET MVC 应用程序中的这个静态文件?

asp.net - MVC 6 @inherit RazorPage

asp.net-mvc - 下拉列表客户端需要验证(无模型)

ASP.NET MVC 删除操作方法中的查询字符串

jquery - 为什么 div 在部分 View 中回发后重复使用相同的 id 和类,MVC

c# - 在回传上追加 QueryString?

c# - Owin 中间件 VS WebAPI DelegatingHandler

c# - 如何在具有指定数据源的 DataGridView 中启用排序?

c# - HttpModule 中的 PreSendRequestHeaders 替代方案