我使用声明为@Html.TextBoxFor(model => model.StartDate, new { type = "datetime-local", @class = "form-control", @style = "height:34px;width:230px" })
的Html.TextBoxFor接受输入,其中有两个都通过AJAX将它们的值传递给C#控制器函数,该函数执行以下操作
DateTime ResourceStart = DateTime.ParseExact(resource_start, "dd/MM/yyyyTHH:mm", CultureInfo.InvariantCulture);
DateTime ResourceDue = DateTime.ParseExact(resource_due, "dd/MM/yyyyTHH:mm", CultureInfo.InvariantCulture);
resource_start
和resource_due
的值分别为“2016-12-02T09:30”和“2016-12-12T12:50”。
以下是程序崩溃时的异常细节。
System.FormatException was unhandled by user code
HResult=-2146233033
Message=String was not recognized as a valid DateTime.
Source=mscorlib
StackTrace:
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
at ITPortal.Areas.ITWorkstream.Controllers.WorkstreamController.CreateResource(Int32 task_id, String resource_start, String resource_due) in c:\Work\ITPortal\ITPortal-DEV\ITPortal\Areas\ITWorkstream\Controllers\WorkstreamController.cs:line 247
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
InnerException:
我认为我的问题在于我在ParseExact函数中为我的字符串声明的格式,有人能告诉我使用instad的格式吗?
答案 0 :(得分:8)
您的格式字符串"dd/MM/yyyyTHH:mm"
与您的输入格式2016-12-02T09:30
不匹配:格式为带有斜线的第一天,而输入是带有破折号的第一年。
您应该将输入更改为02/12/2016T09:30
,或更改格式:
DateTime.ParseExact(resource_due, "yyyy-MM-ddTHH:mm", CultureInfo.InvariantCulture)