Search code examples
c#asp.net-mvcdefaultmodelbinder

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?


Solution

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