简体   繁体   English

具有继承关系的模型的asp.net mvc默认模型绑定程序

[英]asp.net mvc default model binder for model that has inheritance

I have a form that I binding to that has the following structure: 我绑定到的表单具有以下结构:

public class Status
{
   public List<ABCAttachment> ABCAttachments_Files { get; set; }
}

public class Attachment
{
  public string Id { get; set; }
}

public class ABCAttachment : Attachment
{
   string Name { get; set; }
}

My action looks like this: 我的动作如下所示:

public ActionResult SaveAttachment(Status status)
{
  ....
}

The data is coming over in the form 数据以以下形式出现

ABCAttachments_Files[0].Id="0", ABCAttachments_Files[0].Name="test" 

When I access status in my SaveAttachments action the Id is there, but the Name is not. 当我在SaveAttachments操作中访问状态时,ID在那,但名称不在。 I see it correctly being posted, but why is it not binding properly? 我看到它正确地发布了,但是为什么绑定不正确?

Looks like the Name property needs to be public or it won't be bounded to: 看起来Name属性需要是公共的,否则将不受限于:

public class ABCAttachment : Attachment
{
   string Name { get; set; }
}

should be 应该

public class ABCAttachment : Attachment
{
   public string Name { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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