editor.dependent not working

editor.dependent not working

Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

Hello,

I have the following .net core code:

        public ActionResult EntrySlopeSpan()
        {
            .Field(new Field("SlopeSpanEntry.HighwayID")
                .Options(new Options()
                                .Table("v0_Highway")
                                .Value("ID")
                                .Label("HighwayName")                                        
                            )                          

            .Field(new Field("SlopeSpanEntry.DirID")
                .Options(new Options()
                                .Table("Dir")
                                .Value("ID")
                                .Label("Direction")
                            )
                           

        return Json(response);
            }
        

        [Route("/api/sp_dir")]
       
        public ActionResult DirOptions()
        {
            var request = HttpContext.Request;
   
            using (var db = new Database(dbType, dbConnection))
            {
                var query = db.Select(
                    "Dir",
                    new[] { "ID as value", "Direction as label" },
                    new Dictionary<string, dynamic>() { { "HighwayID", request.Query["SlopeSpanEntry.HighwayID"] } }                   
                );

                dynamic result = new ExpandoObject();
                result.options = new ExpandoObject();
                result.options.SlopeSpanEntry.DirID = query.FetchAll();

                return Json(result);
            }
        }


    }
}


editor = new $.fn.dataTable.Editor({

    {
        type: "select",
        label: "Direction:",
        name: "SlopeSpanEntry.DirID"
    }


editor.dependent('SlopeSpanEntry.HighwayID', '/api/sp_dir');

I got the error message:

Failed to load resource: the server responded with a status of 500()

In the above codes, SlopeSpanEntry.DirID field is dependant on field SlopeSpanEntry.HighwayID.

I am puzzled where was the problem. I suspected the flowing two line codes:

new Dictionary<string, dynamic>() { { "HighwayID", request.Query["SlopeSpanEntry.HighwayID"] } }

result.options.SlopeSpanEntry.DirID = query.FetchAll();

Are those supposed to be full names in the editor form or something else.

Any help would be greatly aprreciated.

Best Regards,

John Li

Answers

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Hi John,

    The first issue is that the default Ajax request that Editor makes with dependent() is POST, so you should use request.Form rather than request.Query. Furthermore, the data submitted to the server has a values property which contains the field values.

    So something more like:

    request.Form["values"]["SlopeSpanEntry.HighwayID"]
    

    should hopefully do it.

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hi Allan,

    Thank you for the suggestions. I made the corrections but I still couldn't make it work. I still get a status of 500().

    Here is revised code.

    [Route("api/sp_dir")]
    [HttpPost]
    public ActionResult DirOptions()
    {
                
        var dbType = Environment.GetEnvironmentVariable("DBTYPE");
        var dbConnection = Environment.GetEnvironmentVariable("DBCONNECTION");
               
        using (var db = new Database(dbType, dbConnection))
        {
            var query = db.Select(
                "Dir",
                new[] { "ID as value", "Direction as label" },
                new Dictionary<string, dynamic>() { { "HighwayID", Request.Form["SlopeSpanEntry.HighwayID"] } }                   
            );
    
            dynamic result = new ExpandoObject();
            result.options = new ExpandoObject();
            result.options.SlopeSpanEntry.DirID = query.FetchAll();
    
            return Json(result);
        }
    }
    

    I even replaced Request.Form["SlopeSpanEntry.HighwayID"] with a actual number, and it didn't make any difference. Any more insights will be greatly appreciated.

    Best Regards,

    John Li

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Hi John,

    Is there anything in the response which gives an error message? The browser's network inspector will let you see the response.

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hey Allan,

    Yes. The error is:

    sp_dir 500 Xhr jquery.js:9392.

    By the way

    request.Form["values"]["SlopeSpanEntry.HighwayID"] has a syntax error.

    I tried

    Request.Form["SlopeSpanEntry.HighwayID"] and
    Request.Form["values[SlopeSpanEntry.HighwayID]"]

    Both has no syntax error but neither worked if this line of code is the problem.

    I really appreciate your helps.

    Best Regards,

    John Li

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hey Allan,

    I would like to provide an update on what I have observed. The problem seems to be

    result.options.SlopeSpanEntry.DirID = query.FetchAll();
    

    because of the following error message during debugging:

    System.Dynamic.ExpandoObject' does not contain a definition for 'SlopeSpanEntry'
    

    It seems that ExpandoObject() can not read 'SlopeSpanEntry.DirID' as a whole property name because of the dot '.' Because Editor generally requires a field to be a full name including both the table and column names, there seems to be a tough issue to solve given the two pretty rigid and conflicting restraints.

    To solve the problem I created a method to return Json string based on the values of HighwayID in the http Request without using ExpandoObject(). I have tested the editor.dependent url using a constant of HighwayID. It does return the correct Json options format. However these option won't get reflected in the editor form on the client side. The options for DirID show blank.

    I hope you or somebody else could shed some light on how to make editor.dependent work. It is really a handy feature if it works in .net core environment.

    Best Regards,

    John

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Hi John,

    Apologies - I was thinking with my Javascript head on. For C# try:

    request.Form["values[SlopeSpanEntry.HighwayID]"]
    

    Allan

  • Chandler242Chandler242 Posts: 36Questions: 12Answers: 0

    Hi Allan,

    Thank you for the response.

    Yes. For request request.Form["values[SlopeSpanEntry.HighwayID]"] is correct. But the problem seems to be ExpandoObject() because it can not read SlopeSpanEntry.DirID as the whole field name. It has run error:

    System.Dynamic.ExpandoObject' does not contain a definition for 'SlopeSpanEntry'

    I have ended up writing an different method to return Json without using ExpandoObject. Now everything works fine.

    Thank you for your help. It is greatly appreciated as always.

    Best Regards,

    John Li

Sign In or Register to comment.