.NET Core Editor Libraries - without database

.NET Core Editor Libraries - without database

InsanelyOneInsanelyOne Posts: 8Questions: 2Answers: 0

Is it possible to use the Editor libraries with a direct database connection? My data source is coming from a Web API call.

This question has an accepted answers - jump to answer

Answers

  • InsanelyOneInsanelyOne Posts: 8Questions: 2Answers: 0

    Clarification: Is it possible to use the Editor libraries WITHOUT a direct database connection?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @InsanelyOne ,

    Yep, see the examples here where Editor is being used standalone.

    Cheers,

    Colin

  • InsanelyOneInsanelyOne Posts: 8Questions: 2Answers: 0

    colin, I'm afraid didn't explain myself very well. I've got a .NET MVC application and I was hoping to use the Editor .Net Core library in my controller class.

    For instance, the online examples show something like this:

    var response = new Editor(db, "staff")
    .Model<StaffModel>()
    .Field(new Field("start_date")
    .Validator(Validation.DateFormat(
    Format.DATE_ISO_8601,
    new ValidationOpts { Message = "Please enter a date in the format yyyy-mm-dd" }
    ))
    .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
    .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601))
    )
    .Process(formData)
    .Data();

    In my case, my .NET MVC application does not have direct access to the database. My datasource is an ASP.Net Web API. So I get my data using the HttpClient class. From what I can tell, the Editor() class requires a direct db connection. Am I missing something?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    The examples Colin linked to show DT being used with no db connection.

  • InsanelyOneInsanelyOne Posts: 8Questions: 2Answers: 0

    The example that is being linked to does not make a single reference to ASP.NET MVC.

    This is what I'm talking about: https://editor.datatables.net/manual/net/mvc

    I am not talking about a standalone Editor. I want to use the .NET libraries. I'm trying to determine if I can use a non-database datasource for the Editor() class (this is specific to the MVC controller class on the server side).

    I don't know how else to explain it.

  • allanallan Posts: 64,017Questions: 1Answers: 10,555 Site admin
    edited April 2019 Answer ✓

    Hi,

    From what I can tell, the Editor() class requires a direct db connection. Am I missing something?

    You aren't missing anything and you are correct, the Editor class does indeed use a db connection (an ADO.NET adaptor). It will generate SQL based on the configuration set up for it. I suppose it would be possible to have the database layer work with something other than SQL, but its not something I've really thought about before.

    If you want to use Editor with a preexisting API, then you'd need to write your own server-side integration code. The client / server interface is fully documented and I'm happy to answer any questions you have about it.

    Allan

  • InsanelyOneInsanelyOne Posts: 8Questions: 2Answers: 0

    Thanks Allan. I had already gone down the client/server interface path but was just curious if there was another way.

    This is what I've got so far. I wanted to modify the data going back to the server to include the name of the field that was updated so I added this pre-submit event code:

                     // Editor Pre-submit event to add a value to the postback data to indicate which field was edited
                     quoteDetailEditor.on('preSubmit', function (e, data, action) {
                         var field = quoteDetailEditor.displayed()[0];
                         data["fieldName"] = field;
                         console.log("preSubmit-current field = " + field);
                     });
    

    My postback controller class (bear in mind this code is just proof-of-concept):

            [HttpPost]
            public IActionResult QuoteDetail([FromBody] JObject request)
            {
    
                var rowData = new List<QuoteDetail>();
                var key = string.Empty;
                var action = request.GetValue("action").ToString();
                var fieldName = request.GetValue("fieldName").ToString();
                var data = request.GetValue("data");
                foreach (JProperty item in data)
                {
                    key = item.Name;
                    rowData.Add(item.Value.ToObject<QuoteDetail>());
                }
    
                var errs = new List<FieldError>();
                errs.Add(new FieldError() { Name = "qty", Status = "Invalid Quantity" });
                var result = new QuoteDetailResponse()
                {
                    Data = rowData,
                    Error = "This is an error!!",
                    FieldErrors = errs
                };
                return Json(result);
            }
    

    My response class is defined like this:

        public class QuoteDetailResponse
        {
            public List<QuoteDetail> Data { get; set; }
            public string Error { get; set; }
            public List<FieldError> FieldErrors { get; set; }
        }
    
        public partial class FieldError
        {
            public string Name { get; set; }
    
            public string Status { get; set; }
        }
    

    Let me know if you think I've gone off the rails with this.

  • allanallan Posts: 64,017Questions: 1Answers: 10,555 Site admin

    That looks good to me!

    Allan

This discussion has been closed.