4

I cannot figure out why my model is not being populated. All the data posted is in the Request.Form, but the model is actually turning out to be null.

According to this answer on model with collection not populating on postback

In other words, (...) If any required fields are missing, or if the values are submitted in such a way that they cannot be converted to the type of a required field, then the entire object will be left null

I have changed several value types, but I cannot get it to work.

Here is my model:

public class AddModel
{
    //Get properties 
    public Vehicle vehicle; 

    //Post properties 
    [Required(ErrorMessage = "Please enter a start date")]
    public DateTime StartDate; 
    public int? StatusCode; 

    public SelectList StatusCodes()
    {
         ...
    } 
}

Can you think of why it's not being populated?

Community
  • 1
  • 1
LocustHorde
  • 6,361
  • 16
  • 65
  • 94

2 Answers2

6

Making AddModel members Properties - adding get; set; to fields should solve your problem

archil
  • 39,013
  • 7
  • 65
  • 82
  • Hi, I do not have ``get; set;`` to various other models (update, new, modifyWithBase etcc) and they all work great.. should I add getters and setters to every member property? will try it out.. what is the explanation for this please? – LocustHorde Sep 07 '11 at 14:09
  • What you have in the OP are *fields* and not *properties*. That was my first instinct as well. The difference is subtle, but in general properties can have logic behind them (though they usually don't) whereas fields are raw member variables. The `get;` and `set;` are default implementations of the getter and setter methods. As a rule, I generally use properties for public API and fields for private data that isn't directly exposed (though might be through properties). There likely are more formal best practices online. – bambams Feb 18 '14 at 21:36
2

As per @archil's response, you should make your public variables properties. Although this may work with types you will run into problems as soon as you add complexity. Certainly for classes but possible for nullable types too.

The model binders use reflection to parse the form fields into the model, and reflection works differently on properties to public variables - in the case of these models, the differences will likely be causing the failure here.

Hope that helps - and hat tip to @archil for answering this (probably) correctly, much sooner than me!

Timbo
  • 4,505
  • 2
  • 26
  • 29
  • wow, okay it is working now, I was so not ready to believe that this will solve the problem.. went through the coding horror post and other blogs mentioned on there as well.. there is so much left to learn! – LocustHorde Sep 07 '11 at 15:14
  • now I am confused.. which answer to select?! @archil's answer solved the problem but your answer helped me understand why it solved the problem.. hm.. – LocustHorde Sep 07 '11 at 15:15
  • 3
    If @archil's answer got your model working then it's probably only fair to use his :) – Timbo Sep 07 '11 at 15:17