DateTime对象在ASP.NET MVC [英] dateTime object in ASP.NET MVC

查看:82
本文介绍了DateTime对象在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人使用模型中MVC结合成功绑定2文本框,一个日期时间属性,我试过斯科特的方法的http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx但是不满,因为这将停止具有相同名称(以便验证无法设置正确的CSS如果失败)。

Has anyone successfully bound 2 textboxes to one DateTime property using the model binding in MVC, I tried Scott's method http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx but was dissatisfied as this stops the HTML fields and Model properties having the same name (so the validation could not set the correct css if it failed).

我现在尝试通过从BindingContext中删除ValueProviderResult对象和增加从日成果和TIEM(使用后的Scotts的。时间约定)组成的关键一个新的修改,但我是一个有点担心乱搞用的BindingContext对象直接。

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

我们的想法是,我可以使用IDateErrorInfo和VAB PropertyComparisonValidator在其中一个必须晚于其他型号相比2日期时间,要做到这一点的时候元素需要被包括在内。

The idea is that i can use IDateErrorInfo and VAB PropertyComparisonValidator to compare 2 datetimes on the model where one needs to be later than the other, to do this the time element needs to be included.

推荐答案

我用不同的方法,去两套不同的型号:我的视图模型将有两个属性,并为这些领域的验证,而我的域模型会有一个日期时间。然后后绑定,我让视图模型更新域:

I use a different approach and go for two different sets of models: My view model would have two properties and the validation for those fields, while my domain model would have one DateTime. Then after the binding, I let the view model 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; }
}

这篇关于DateTime对象在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆