DateTime object in ASP.NET MVC

Someone successfully associated 2 text fields with one DateTime property using model binding in MVC, I tried the Scott method http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx , but was unsatisfied since it stops the HTML fields and properties models with the same name (therefore, verification cannot establish the correct css if it failed).

My current attempt modifies this by removing the ValueProviderResult object from the bindingcontext file and adding a new one for the key made up of the date result and tiem (using the .Time convention in post Scotts), but I'm a little careful about messing with the direct bindingContext object.

The idea is that I can use IDateErrorInfo and VAB PropertyComparisonValidator to compare 2 dates on a model where you need to be later than another, for this you need to add a time element.

+7
datetime asp.net-mvc
Apr 7 '09 at 15:51
source share
1 answer

I take a different approach and move on to two different sets of models: my view model will have two properties and validation for these fields, while my domain model will have one DateTime. Then, after binding, I allow the view model to update the domain:

public ActionResult Update(DateInput date) { if(date.IsValid) { var domain = someRepository.GetDomainObject(); // not exactly, but you get the idea. date.Update(domain); } // ... } public class DateInput { public string Date { get; set; } public string Time { get; set; } public void Update(DomainObject domain) { ... } } public class DomainObject { public DateTime SomePointInTime { get; set; } } 
+3
May 03 '09 at 20:47
source share



All Articles