简体   繁体   中英

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

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

should be

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

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