ASP.NET MVC中的dateTime对象

Pha*_*bus 7 asp.net-mvc datetime

有没有人使用MVC中的模型绑定成功地将2个文本框绑定到一个DateTime属性,我尝试了Scott的方法http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx但不满意,因为这会停止HTML字段和具有相同的名称(如果失败,验证无法设置正确的CSS).

我当前的尝试通过从bindingcontext中删除ValueProviderResult对象并为日期结果和一个tiem(使用Scotts帖子中的.Time约定)组成的键添加一个新对象来修改它.但我有点担心乱搞使用bindingContext对象直接.

我的想法是,我可以使用IDateErrorInfo和VAB PropertyComparisonValidator来比较模型上的2个日期时间,其中一个需要晚于另一个,为此需要包含时间元素.

Tho*_*yde 3

我使用不同的方法并采用两组不同的模型:我的视图模型将具有两个属性和这些字段的验证,而我的域模型将具有一个 DateTime。然后在绑定之后,我让视图模型更新域:

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; }
}
Run Code Online (Sandbox Code Playgroud)