Error passing parameters to Editor global validators

Error passing parameters to Editor global validators

htwyfordhtwyford Posts: 23Questions: 9Answers: 1
edited June 2017 in Free community support

I have a very simple example of a .NET server-side method with a global validator, as specified here. For the sake of simplicity, I made my example exactly the same as the example on that page:

public JsonResult EditorExample(int id)
{
    using (var db = new DataTables.Database("sqlserver", db.Database.Connection.ConnectionString))
    {
    // Allow read only access based on a session variable
    var response = new Editor(db, "table")
        .Model<TableModel>()
        .Validator( (editor, action, data) {
            if ( action != DtRequest.RequestTypes.DataTablesGet && Session["ReadOnly"] ) {
                return "Cannnot modify data";
            }
            return "";
        } )
        .Process(request)
        .Data();
        return Json(response, JsonRequestBehavior.AllowGet);
    }
}

I am having a number of issues with compiling the .Validator() method. The first issue is with the (editor, action, data) parameters; I get an error saying that they don't exist in this context. To resolve this, I specify their types:

 .Validator((Editor editor, DtRequest.RequestTypes action, DtRequest data) 

when I do this, I get three different compile errors:
1. Argument 1: Cannot convert from '(DataTables.Editor, DataTables.DtRequest.RequestTypes, DataTables.DtRequest)' to 'Func<Editor, DtRequest.RequestTypes, DtRequest, string>'
2. A declaration is not allowed in this context.
3. Use of unassigned local variable editor/action/data.

I have a general idea of what each of these errors mean, but I don't know how to fix them.
1. Going through the Editor source, I can see that the .Validator method does indeed expect Func<Editor, DtRequest.RequestTypes, DtRequest, string>. But reading about this kind of error online, it seems as if .NET should see that I'm passing Editor, DtRequest.RequestTypes, DtRequest and it should solve this kind of thing on its own
2. I don't really know what's up with this one. Why can't I declare variables inside an Editor declaration? I can declare other variables...
3. This would suggest that I am declaring them, but they don't actually exist in memory as Editor/DtRequest.RequestTypes, DtRequest types.

I'm pretty lost here. Is this a common issue? Is it related to my .NET installation? Any help appreciated!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,126Questions: 1Answers: 10,579 Site admin
    Answer ✓

    That's embarrassing - sorry.

            .Validator( (editor, action, data) {
    

    should be using a lambda function:

            .Validator( (editor, action, data) => {
    

    I'll get that syntax error fixed in the documentation right now - thanks!

    Allan

This discussion has been closed.