微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。