asp mvc格式的时间跨度(mm:ss)textboxfor可以使用bootstrap timepicker

时间:2015-09-11 12:21:11

标签: c# jquery asp.net-mvc twitter-bootstrap formatting

我已经阅读了几个有类似问题的帖子,但我仍然无法让它正常工作。我的问题是将两个timepan属性格式化为mm:ss,并让它们正确地使用this bootstrap datepicker。

模型属性:(不确定它是否正确)

Ctrl+Shift+A/ ⌘ + Shift + A

观点:

[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:mm\:ss}")]
public TimeSpan AgentHoldoffTime { get; set; }

[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:mm\:ss}")]
public TimeSpan AgentAlertTime { get; set; }


public InteractionQueueModel() // ctor
{
     AgentAlertTime = new TimeSpan(0, 0, 17);
     AgentHoldoffTime = new TimeSpan(0, 2, 00);
}

JS:

@Html.EditorFor(model => model.QueueModel.AgentHoldoffTime, new { htmlAttributes = 
new { @class = "form-control timepicker-holdOffTime" } })

@Html.EditorFor(model => model.QueueModel.AgentAlertTime, new { htmlAttributes = 
new { @class = "form-control timepicker-alertTime" } })

现在,当视图呈现时,文本框会显示var timePickerAlertTime = $(".timepicker-alertTime"), timePickerHoldOffTime = $(".timepicker-holdOffTime"); timePickerAlertTime.datetimepicker({ format: "mm:ss", }) .on("dp.change", function () { ... }); timePickerHoldOffTime.datetimepicker({ format: "mm:ss", }) .on("dp.change", function () { ... }); 列的00:17,其值为time(7)。一切似乎都很好,但是当我提交00:00:17.0000000时,它会保存为01:00而不是01:00:00.0000000?我可能错过了一些简单的事情,但我无法弄明白。

提前致谢。

2 个答案:

答案 0 :(得分:0)

当你使用'EditorFor'时,框架会尝试为数据类型呈现最佳的HTML元素,在TimeSpan的情况下,最好的是<input type="time"/>,因为我知道你需要处理秒数,但这个输入是默认不考虑秒数,您需要使用属性step = 1“强制”。现在,如果您使用'EditorFor',它不允许您设置属性,或者至少我还没有找到如何做到这一点,所以将其更改为'TextBoxFor':

@Html.TextBoxFor(model => model.AgentHoldoffTime, new
                {
                    @class = "form-control timepicker-holdOffTime",
                    type = "time",
                    step = 1
                })

我希望这个帮助

答案 1 :(得分:0)

指定格式适用于我:

@Html.TextBoxFor(m => Model.AgentHoldoffTime,
    "{0:hh\\:mm}",
    new { 
       @class = "form-control",
       placeholder = "hh:mm",
})

这个签名:

//
// Summary:
//     Returns a text input element.
//
// Parameters:
//   htmlHelper:
//     The HTML helper instance that this method extends.
//
//   expression:
//     An expression that identifies the object that contains the properties to display.
//
//   format:
//     A string that is used to format the input.
//
//   htmlAttributes:
//     An object that contains the HTML attributes to set for the element.
//
// Type parameters:
//   TModel:
//     The type of the model.
//
//   TProperty:
//     The type of the value.
//
// Returns:
//     An input element whose type attribute is set to "text".
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
                                this HtmlHelper<TModel> htmlHelper,
                                Expression<Func<TModel, TProperty>> expression,
                                string format,
                                object htmlAttributes);