System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'

Habe ich aufgeklebt das Debuggen dieses Problems. Ich glaube, dass der Fehler Auftritt, wenn ich versuche, füllen Sie eine Dropdown-Liste.

 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;

Stack-Trace

[InvalidCastException: Unable to cast object of type 'System.Int32' to type '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
  • Das problem könnte sein, in Ihrer Besetzung. Irgendeinen Grund, warum Sie die "Select *"? Warum nicht einfach die db.Kunden.ToList() da willst du die ganze Liste?
  • Was ist das problem mit der Besetzung? es wird nicht lassen Sie mich halten Sie Es als nur-Text = Wert.CustomerID. In Bezug auf die wählen Sie * ich den Fehler beheben, das war ein guter Punkt
  • Ich meinte Ihre Stimmen, um die Customer-Objekt, das sollte unnötig sein, wenn Sie vermeiden, die SqlQuery. Das Auffüllen der Liste mithilfe der ToString() sollte in Ordnung sein.
  • Danke, aber noch kein Glück
  • zeigen Sie den stack-trace
  • Ich habe die Spur

InformationsquelleAutor kayze | 2013-10-06
Schreibe einen Kommentar