gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 中的 dateTime 对象

转载 作者:行者123 更新时间:2023-12-04 12:44:21 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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; }
}

关于asp.net-mvc - ASP.NET MVC 中的 dateTime 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/726433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com