DateTime的财产作为视图中的两个输入,一个日期和第二时间部分 [英] DateTime property as two inputs on view, one for date and second for time part

查看:118
本文介绍了DateTime的财产作为视图中的两个输入,一个日期和第二时间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可以在模型中的一个日期时间属性,但是用它作为视图/表上的两个输入?例如日期部分将使用jQueryUI的日期选择器,而一部分时间选择器将是一个蒙面输入,或其他漂亮的jQuery插件。不时,我需要用两个dropdows时间采摘(小时和分钟)。

Is this possible to have one DateTime property in model, but use it as two inputs on view/form? For example Date part will be using jqueryui datepicker, while Time part picker will be a masked input, or other nifty jquery plugin. From time to time i need to use two dropdows for time picking (hours and minutes).

我的目标是在模型中的一个日期时间属性(日期和时间没有部分字符串)。这可能吗?使用MVC4。

My goal is to have one DateTime property in model (no partial strings for date and time). Is this possible? Using MVC4.

推荐答案

下面是一个技术,我使用的日期分为3个领域,而只有有对视图模型1日期时间属性。虽然不完全是你以后,你应该能够使用类似的方法来达到你想要的东西。

Here's a technique i use to split a date into 3 fields, while only having 1 DateTime property on the ViewModel. While not exactly what you're after, you should be able to use a similar method to achieve what you want.

编辑模板
/views/shares/editortempaltes/datetime.cshtml

@model Nullable<System.DateTime>         
@Html.TextBox("Day", Model.HasValue ? Model.Value.Day.ToString() : "", new { Type = "Number", @class="date-day" })
@Html.ValidationMessage("Day")
@Html.DropDownList("Month", months, new { @class="date-month" })
@Html.ValidationMessage("Month")
@Html.TextBox("Year", Model.HasValue ? Model.Value.Year.ToString() : "", new { Type = "Number", @class="date-year" })
@Html.ValidationMessage("Year")

自定义模型绑定器

public object GetValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
    int day, month, year;

    if (TryGetValue(controllerContext, bindingContext, propertyDescriptor.Name + ".Day", out day) && TryGetValue(controllerContext, bindingContext, propertyDescriptor.Name + ".Month", out month) && TryGetValue(controllerContext, bindingContext, propertyDescriptor.Name + ".Year", out year))
    {
        try
        {
            return new DateTime(year, month, day);
        }
        catch (ArgumentOutOfRangeException)
        {
            var fullPropertyName = bindingContext.ModelName + "." + propertyDescriptor.Name;               
            bindingContext.ModelState[fullPropertyName] = new ModelState();                         
            bindingContext.ModelState[fullPropertyName].Errors.Add("Invalid date");
        }
    }
    return null;
}


private bool TryGetValue(ControllerContext controllerContext, ModelBindingContext bindingContext, string propertyName, out int value)
{
    var fullPropertyName = bindingContext.ModelName + "." + propertyName;
    string stringValue = controllerContext.HttpContext.Request[fullPropertyName];
bindingContext.ModelState.Add(fullPropertyName, new ModelState() { Value = new ValueProviderResult(stringValue, stringValue, null) });
    return int.TryParse(stringValue, out value);
}

用法
一个DateTime属性添加到您的视图模型,然后调用

Usage Add a DateTime property to your ViewModel, then call

@Html.EditorFor(m => m.DateTimeProperty)

这篇关于DateTime的财产作为视图中的两个输入,一个日期和第二时间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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