MVC 5 时间输入字段设置和存储时间

问题描述

我有一个 MVC 应用。

调用一个数据库,在数据集中获取我的数据并将其存储在我的类中。

这里是我如何从数据集中存储我的数据

    public ActionResult Recovery()
    {
        List<PersonModel> lmd = new List<PersonModel>(); 
        DataSet ds = new DataSet();
        Connection.Connection con = new Connection.Connection();
        ds = con.mydata(); 

        foreach (DaTarow dr in ds.Tables[0].Rows)  
        {
            lmd.Add(new PersonModel
            {
                t_hrs = dr["hrs"] == dbnull.Value ? (DateTime?)null : (DateTime?)dr["hrs"],sID = Convert.ToInt32(dr["sID"]),});
        }

        return View(lmd);
    }

MysqL 返回的字段“hrs”为空,没关系。

当我尝试在“小时”字段中存储 11:11 小时:分钟值时,该值正确存储在数据库中。

但是当我返回网页时出现以下错误

"system.invalidCastException: '指定的转换无效。'"

在控制器这一行

lmd.Add(new PersonModel

数据库中记忆的时间值为11:11:00.00000

字段类型是

`hrs` time(6) NULL DEFAULT NULL,

我试过了,没有成功编辑

t_hrs = dr["hrs"] == dbnull.Value ? (DateTime?)null : (DateTime?)dr["hrs"],

t_hrs = dr["hrs"] == dbnull.Value ? (DateTime?)null : dr.Field<DateTime>("hrs"),

如何解决这个错误?谢谢

- $exception {"Invalid cast specified."} system.invalidCastException + Data {System.Collections.ListDictionaryInternal}
System.Collections.IDictionary {System.Collections.ListDictionaryInternal} HResult -2147467262 int HelpLink null string + InnerException null System.Exception Message "Invalid cast specified." string Source "System.Data.DataSetExtensions" string StackTrace " in System.Data.DaTarowExtensions.UnBoxT`1.ValueField(Object value)\r\n
in System.Data.DaTarowExtensions.Field[T](DaTarow row,String columnName)\r\n
in Ins.Controllers.HomeController.Recovery()
in C:\\source\\repos\\Ins\\Ins\\Controllers\\HomeController.cs:line 439\r\n
in lambda_method(Closure,ControllerBase,Object[] )\r\n
in System.Web.Mvc.ActionMethoddispatcher.Execute(ControllerBase controller,Object[] parameters)\r\n
in System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2 parameters)\r\n
in System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2 parameters)\r\n
in System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult,ActionInvocation innerInvokeState)\r\n
in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)\r\n
in System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()\r\n
in System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)\r\n
in System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__displayClass11_0.<InvokeActionMethodFilterasynchronouslyRecursive>b__0()\r\n
in System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__displayClass11_2.<InvokeActionMethodFilterasynchronouslyRecursive>b__2()" string + TargetSite {T ValueField(System.Object)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)