I'll start off by saying my terminology might be off, which is why I might be having trouble finding an answer to this question, and apologising for the wall of text.
I have a strongly-typed view that inherits from a custom type called Record. Record has a dictionary of named fields of varying datatypes - some strings, some ints, some DateTime/TimeSpans.
Via a dropdownlist onchange on a form, I get to a HttpPost Action method in my controller - as I understand it, the model of the current view is supposed to be 'passed through' to the controller method. It appears to be doing this, but only partially: the DateTime and TimeSpan fields of the Record are coming through as their default values. All the string, int etc fields are coming through fine.
I debugged through the Record code during the databinding that happens in the back end as the HttpPost controller method is called and it seems that a new, blank Record is constructed, then various properties are set - but if as part of constructing a 'blank' record a property is initialised to some valid, non-null value, the Set methods of those properties are never called.
My question is - what's actually happening in this databinding phase? Is it as I described it? Do I have to make all properties of my Record initialise as nulls to get them to databind properly?
Edit: The Record (simplified)
public class Record
{
public Record() : base()
{
fields.Add("Id", new FieldValue { DataType = typeof(int) });
fields.Add("StartDateTime", new FieldValue { DataType = typeof(DateTime) });
}
private Dictionary<string, FieldValue> fields = new Dictionary<string, FieldValue>();
public Dictionary<string, FieldValue> Fields
{
get
{
return fields;
}
}
public long? Id
{
get
{
FieldValue fieldValue = Fields["Id"];
return fieldValue != null ? (long?)fieldValue.Value : null;
}
set
{
SetFieldValue("Id", value);
}
}
public DateTime StartDateTime
{
get
{
FieldValue fieldValue = Fields["StartDateTime"];
if (fieldValue == null || fieldValue.Value == null)
{
return DateTime.MinValue;
}
else
{
return (DateTime)fieldValue.Value;
}
}
set
{
SetFieldValue("StartDateTime", value);
}
}
protected void SetFieldValue(string fieldName, object value)
{
Fields[fieldName] = new FieldValue(value);
}
}
The value class for the dictionary of FieldValues:
public class FieldValue
{
internal FieldValue(object value)
{
DataType = value.GetType();
Value = value;
}
internal FieldValue()
{ }
public Type DataType { get; set; } //The data type of the field
public object Value { get; set; } //The value of the field
}
I display a strongly-typed record view that shows StartTime in a form, but not Id. When a HttpPost happens, the controller method that handles HttpPosts for that view recieves a record that has the same Id as the original Record, but a StartDateTime of DateTime.MinValue.
Edit2: Does the way I'm displaying any model fields in the view have anything to do with what the controller HttpPost method will 'see' of the model?