Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc System.InvalidCastException:无法将“System.Int32”类型的对象强制转换为“System.String”类型_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net mvc System.InvalidCastException:无法将“System.Int32”类型的对象强制转换为“System.String”类型

Asp.net mvc System.InvalidCastException:无法将“System.Int32”类型的对象强制转换为“System.String”类型,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我一直在尝试调试这个问题。我相信错误是在我尝试填充下拉列表时发生的 IEnumerable<Customer> values = db.Customers.SqlQuery("SELECT * FROM Customer").ToList().Cast<Customer>(); IEnumerable<SelectListItem> items = from value in values select new SelectListI

我一直在尝试调试这个问题。我相信错误是在我尝试填充下拉列表时发生的

 IEnumerable<Customer> values = db.Customers.SqlQuery("SELECT * FROM Customer").ToList().Cast<Customer>();

 IEnumerable<SelectListItem> items =
     from value in values
     select new SelectListItem
     {
         Text = value.CustomerID.ToString(),
         Value = value.CustomerID.ToString(),
     };

 ViewBag.Accounts = items;
堆栈跟踪

[InvalidCastException:无法将类型为'System.Int32'的对象强制转换为类型为'System.String'

System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(Object value) +46
   System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext) +115
   System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext) +29
   System.Web.Mvc.<Validate>d__15.MoveNext() +161
   System.Web.Mvc.<Validate>d__1.MoveNext() +311
   System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +136
   System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +66
   System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1314
   System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +416
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +446
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +302
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +382
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +317
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +130
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +249
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

从异常消息判断,您的Customer类如下所示

public class Customer
{
    [StringLength(4)]
    public int CustomerID { get; set;}
}
请注意,不能在int属性上使用StringLength


去掉StringLength,验证异常就会消失

问题可能出在你的演员阵容上。你为什么要选择*?既然你想要完整的列表,为什么不只是db.Customers.ToList?演员阵容有什么问题?它不允许我将其保持为Text=value.CustomerID。关于select*I修复了这个问题,这是一个很好的观点,我指的是对Customer对象的强制转换,如果您避免使用SqlQuery,这应该是不必要的。使用ToString填充列表应该没问题。谢谢,但是luckI仍然没有发布traceThanks!这也是我的问题。那我怎么能这样做呢?对于Regex?@equiman,这里不是提出后续问题的地方,我无法在评论中完全回答您,您也无法正确地提出问题。但是,您可能正在查找[RangeAttribute]。否则,您的int可能不是int。谢谢呵呵。。。我在数据库端更改了属性的类型,但忘记在代码中的分部类中更改它。在将其更改为int之前,它是一个字符串,并且它确实有StringLength数据注释。这很有趣。我也遇到了类似的问题,因为一个属性上的MaxLength属性过去是字符串,但后来被重构到它自己的视图模型中。不使用StringLength4,只需使用Rangeint min,int max属性即可。