I'm new to datatables editor but am getting Unable to cast value to be string....

I'm new to datatables editor but am getting Unable to cast value to be string....

jimlocignojimlocigno Posts: 3Questions: 1Answers: 0
edited May 2022 in Free community support

Error Message:

ystem.Exception
  HResult=0x80131500
  Message=Unable to cast value to be String
  Source=DataTables-Editor-Server
  StackTrace:
   at DataTables.EditorUtil.NestedData.WriteCast(Dictionary`2 outData, String name, Object value, Type type)
   at DataTables.EditorUtil.NestedData.WriteProp(Dictionary`2 outData, String name, Object value, Type type)
   at DataTables.Field.Write(Dictionary`2 outData, Dictionary`2 srcData)
   at DataTables.Editor._Get(Object id, DtRequest http)
   at DataTables.Editor._Process(DtRequest data)
   at DataTables.Editor.Process(DtRequest data)
   at DataTables.Editor.Process(HttpRequest request, String culture)
   at wrklogtrust_work_log_trust.Areas.LookupTables.Controllers.Category.dteditorcategory() in C:\Code_Repos\WRKLOGTRUST_work_log_trust\wrklogtrust_work_log_trust\Areas\LookupTables\Controllers\Category.cs:line 225
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

` var dbType = "mysql";
            using (var db = new Database(dbType, DbConnection))
            {
                var response = new Editor(db, "category", "idCategory")

                    .Model<DataTablesEditorCategoryDetails>()
                      .Field(new Field("idCategory")
                      
                    )

                    .Field(new Field("category_a")

                        .Validator(Validation.NotEmpty())
                    )
                     .Field(new Field("category_b")

                    )
                      .Field(new Field("category_c")

                    )
                       .Field(new Field("category_d")

                    )
                    .Field(new Field("is_active"))
                    .Field(new Field("is_billable"))

                    .Field(new Field("sort_order")
                        .Validator(Validation.Numeric())
                    )
                    .Field(new Field("default_short_description")


                    )
                      .Field(new Field("default_start_time")


                    )
                        .Field(new Field("default_stop_time")

                    )
             
                    .TryCatch(false)
                    .Process(Request)
                    .Data();

                return Json(Response);
            }
        } `

Model:

 ` public class DataTablesEditorCategoryDetails
    {

        public int idCategory { get; set; }
        public string category_a { get; set; } = "";
        public string category_b { get; set; } = "";
        public string category_c { get; set; } = "";
        public string category_d { get; set; } = "";

        public bool is_active { get; set; }
        public bool is_billable { get; set; }

        public int sort_order { get; set; }
        public string default_short_description { get; set; } = "";
        public string default_start_time { get; set; } = "";
        public string default_stop_time { get; set; } = "";
    } `

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Could you give some more information, please - such as where do you see this error, is it on table load, or when you're creating/editing a record?

    Colin

  • jimlocignojimlocigno Posts: 3Questions: 1Answers: 0

    Hi Colin,

    It's on the table load.

    My table doesn't even display (other than the table headers)

  • jimlocignojimlocigno Posts: 3Questions: 1Answers: 0

    I was able to use the editor generator and compare that to what I had - I got passed the issue in this post but now I have a different issue. I'm going to see what I can figure out myself first.

  • allanallan Posts: 61,627Questions: 1Answers: 10,090 Site admin

    Hi,

    Thanks for the update. Out of interest, what was it in the end? Normally that error would indicate a parameter type in the model which can't be represented in JSON, but your model looks okay there.

    What is the latest issue? I might be able to help resolve it.

    Allan

  • JormJorm Posts: 16Questions: 4Answers: 0

    In case someone comes to this thread and all the obvious things like model parameter types and database field types seem to be ok: I got this error message because I was representing a field twice by mistake on the controller side of things - I'm using .NET model-view-controller (note how the Field(new Field("MatlMarkupFraction")) bit is listed twice):

          .Field(new Field("LinerUnitCost"))
          .Field(new Field("LinerTotalCost"))
          .Field(new Field("MatlMarkupFraction"))
          .Field(new Field("TotalMatlCost"))
          .Field(new Field("MarkedUpMatlCost"))
          .Field(new Field("MatlMarkupFraction"))                            
          .Field(new Field("ForTrimbleLotNumber"))
    
  • allanallan Posts: 61,627Questions: 1Answers: 10,090 Site admin

    Ah! Nice spotting. Thanks for letting us know about that. I'll have a look and see if I can make a more explicit error about that (the client-side does have an explicit error for such as state).

    Allan

Sign In or Register to comment.