我对 datetimeoffset 的模型状态有疑问。在以下位置看到了答案:Modelstate 总是抛出无效的日期时间错误,但这些并不能解决我的问题。
我在模型中有 FromDate:
[Required, Display(Name = "From")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTimeOffset FromDate { get; set; }
我在我的视图中显示它:
<div class="editor-label">
@Html.LabelFor(O => O.FromDate)
-- 开始-- 输入 data-val="true" id="FromDate" name="FromDate" type="datetime" class="datetime" data-datetime="" value ="@Model.FromDate.ToString(" r")" ---结束
@Html.ValidationMessageFor(O => O.FromDate)
</div>
将 jquery 中的 datetime 值更改为 jquery 中的本地时间:
if ($(this.element).val() != "") {
// If we wont specify time on recreate then time sliders will stay unchanged.
// we manipulate datepicker value and value of input to display differently.
// LLLL--> Thursday, April 18 2013 1:20 PM
// L --> 04/18/2013
// LT --> 8:30 PM
this._Input.datepicker("setDate", new Date(moment($(this.element).val()).format("LLLL")));
this._Input.val(moment($(this.element).val()).format("L LT"));
}
但是,当我编辑和保存时,我收到“FromDate is not valid”异常。
“值 'Fri Jun 14 05:30:00 UTC+0530 2013' 对 From 无效。”
有人可以建议如何解决吗?
模型粘合剂:
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
//var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
string s = bindingContext.ValueProvider.GetValue("FromDate").AttemptedValue;
string value = bindingContext.ValueProvider.GetValue("ThruDate").AttemptedValue;
DateTimeOffset from;
DateTimeOffset thru;
if (s.IndexOf("UTC") != -1)
{
from = DateTimeOffset.ParseExact(s, "ddd MMM dd HH:mm:ss \"UTC\"zzz yyyy", CultureInfo.InvariantCulture);
}
else
{
from = DateTimeOffset.ParseExact(s, "ddd, dd MMM yyyy HH:mm:ss \"GMT\"", CultureInfo.InvariantCulture);
}
if (value.IndexOf("UTC") != -1)
thru = DateTimeOffset.ParseExact(value, "ddd MMM dd HH:mm:ss \"UTC\"zzz yyyy", CultureInfo.InvariantCulture);
else
thru = DateTimeOffset.ParseExact(value, "ddd MMM dd yyyy HH:mm:ss \"GMT\"zzz \"(India Standard Time)\"", CultureInfo.InvariantCulture);
return PrFactory.Create(Convert.ToInt32(bindingContext.ValueProvider.GetValue("PartyRoleTypeId").AttemptedValue),
Convert.ToInt32(bindingContext.ValueProvider.GetValue("PartyId").AttemptedValue),
from,
Convert.ToBoolean(bindingContext.ValueProvider.GetValue("IsSoftDeleted").AttemptedValue),
new Party(),
new PartyRoleType(),
thru);
//return base.CreateModel(controllerContext, bindingContext, modelType);
}