C# - MJoin
C# - MJoin
Hi,
I've been struggling all day with an MJoin in C# using DataTables Editor .NET (version 2.0.5), but I keep getting the error:
Invalid column name 'ProductStorageLocations'.
I've followed the official guide (https://editor.datatables.net/manual/net/mjoin) and also tried using nested classes as suggested, but without success.
Here’s my code:
public JsonResult Product()
{
var request = System.Web.HttpContext.Current.Request;
using (var db = new Database("sqlserver", "...")
{
var editor = new Editor(db, "Products", "Products.Id")
.Model<ProductEditorDto>("Products")
//...(normal fields here)
.MJoin(new MJoin("ProductStorageLocations")
.Link("Products.Id", "ProductStorageLocationProducts.Product_Id")
.Link("ProductStorageLocations.Id", "ProductStorageLocationProducts.ProductStorageLocation_Id")
.Model<ProductStorageLocationDto>()
.Field(new Field("Id")
.Options("ProductStorageLocations", "Id", "Name")
)
)
.Debug(true);
var response = editor.Process(request).Data();
var jsonResult = Json(response, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
}
And my DTOs:
public class ProductStorageLocationDto
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class ProductEditorDto
{
// all normal fields here
public List<ProductStorageLocationDto> ProductStorageLocations { get; set; }
// Also tried with:
//public class ProductStorageLocations
//{
// public Guid Id { get; set; }
// public string Name { get; set; }
//}
}
The table and key names are correct, but the error persists.
If I change the property name in ProductEditorDto from ProductStorageLocations to something else (e.g., XY), the error message changes accordingly: Invalid column name 'XY'.
If I remove the property from the DTO I get:
Incorrect syntax near '='.
Additionally, when I try updating to version 2.2.2, I get a different error, unrelated to MJoin:
Method not found: 'System.String[] System.String.Split(Char, System.StringSplitOptions)'.
Any insights into what might be going wrong?
Thanks!
Answers
Could you show me the JSON response from the server when you attempt to load the data please (the network inspector in your browser will give you access to that)? You have
.Debug(true)
so it will have the SQL that is being generated which will hopefully give me a clue as to what is going wrong.Does it give you any other information, such as the location of where that is being thrown? You could add
.TryCatch(false)
before the.Process()
call which might give a proper backtrace so I can see what is going wrong there.Many thanks,
Allan
Dear Allan,
I solved! The problem was "dbo." in front of "ProductStorageLocations" (which is not present in my previous message but was indeed present in my code).
It seems that the editor can't correctly handle "dbo.tableName", correct?
It probably should! But based on your findings, it sounds like no, it doesn't. I'll need to look into that.
Allan