Search code examples
asp.net-mvc-3viewmodelactionmethod

MVC Action has missing data


I send my viewmodel from my get action method to a view for update, the view has a submit button which takes returns control to the post action method. The viewmodel is of this form

public class MyViewModel
{
   public someObject a;
   public someOtherObject b;
}

Now, in the get method someOtherObject b has data in it, in the view it has data in it, but in the post method it is null. Why might this be?

Thanks,


Solution

  • Without looking into it in too much detail, I'd take a guess at the problem being that your model has fields instead of properties. Try this instead:

    public class MyViewModel
    {
       public someObject a { get; set; }
       public someOtherObject b { get; set; }
    }
    

    The default MVC model binder examines your model for settable properties (hence the overridable SetProperty() method taking a PropertyDescriptor) and sets those values, so I'm guessing that fields are ignored.