Here is my Model, Complex Type Class, and Controller Action: As the code currently stands, UpdateModel(model) will work perfectly fine when all I have for model properties are simple types (i.e. public int number {get; set;}).
Furthermore I have confirmed that my Input values are properly being posted back to the server and exist in the FormCollection.
I should also note that UpdateModel(model) is not throwing any errors for me to troubleshoot. All it does is return the same values I initialized the property with. Thus I'm feeling pretty stuck.
Any ideas would be greatly appreciated, at this point my ASP.net MVC 4 book is lacking the details I need to trouble shoot this problem. Thanks in advance! :)
Model -
public class HomeModel : BaseModel, IModel
{
public ComplexTypeSchema CTS { get; set; } // <-- this property does not update.
public HomeModel()
{
CTS = new ComplexTypeSchema
{
Property1 = Convert.ToDateTime("1/1/2014"),
Property2 = DateTime.Today,
Property3 = 1.5,
Property4 = ""
};
}
}
Complex Type Class -
public class ComplexTypeSchema
{
public DateTime Property1 { get; set; }
public DateTime Property2 { get; set; }
public double Property3 { get; set; }
public string Property4 { get; set; }
public int Property5 { get; set; }
public int Property6 { get; set; }
public double Property7 { get; set; }
public ComplexTypeSchema()
{
}
public ComplexTypeSchema Calculate()
{
this.Property5 = (this.Property2 - this.Property1).Days;
this.Property6 = (int)(this.Property5 * this.Property3);
this.Property4 = this.Property1.AddDays(this.Property6).ToShortDateString();
this.Property7 = ((double)this.Property5 / this.Property6) * 100;
return this;
}
}
Controller Action -
[HttpPost]
public ActionResult Index(FormCollection values)
{
HomeModel model = null;
string viewToReturn = string.Empty;
try
{
model = new HomeModel();
UpdateModel(model.CTS);
}
catch (RulesException e)
{
e.AddExceptionsToModelState(ModelState);
viewToReturn = string.Empty;
}
catch (SystemException e)
{
string message = "Error trying to update model";
ModelState.AddModelError("Error", message);
Log.Error(message, e);
viewToReturn = model.DefaultViewForError;
}
return View(viewToReturn, model);
}