Datatables MVC .net Entity Framework

Datatables MVC .net Entity Framework

Godrules500Godrules500 Posts: 25Questions: 13Answers: 0

Hello, I am coming off of just using plain ole ajax to work with DataTables, but have recently decided to try out the paid editor. However, I am running into some difficulties. Currently I am trying to get it to pull info from the db, however it won't take my db context. Is there anyway to get it to work with this?

Also, is there a place I can find that show's a fully written mvc version? That way I can follow that (controller, view and model)? I imagine you call it using ajax still?

Answers

  • allanallan Posts: 63,352Questions: 1Answers: 10,443 Site admin

    however it won't take my db context

    I'm not entirely sure what you mean by this point I'm afraid. Do you have a ADO.NET connection already that you want to use?

    Also, is there a place I can find that show's a fully written mvc version?

    The .NET examples of Editor focus on Web API because that approach works perfectly with Editor - i.e. plain HTML and Javascript with Ajax calls to get / set data. You can use MVC style with Editor but its really just doing the same as the Web API approach with the controller returning JSON.

    Regards,
    Allan

  • Godrules500Godrules500 Posts: 25Questions: 13Answers: 0
    edited September 2016

    Guess I'm trying to figure out how to call the controller method. Do I call the controller method that contains "new Editor()" code via ajax, or do I call it when navigating from one page to the other? Also, since this is server side, do I need $.fn.dataTable.Editor() and $("#example").DataTable() or is this all handled inside the $.fn.dataTable.Editor()?

    I have my setup like below, but it is not calling my controller method LoadAdministrators().

      var editor = new $.fn.dataTable.Editor({
                    url: "@Url.Action("LoadAdministrators", "Home")",
                    contentType: "application/json; charset=utf-8",
                    table: '#administrator',
                    fields: [
                        {
                            "label": "name:",
                            "name": "name"
                        }
                    ]
                });
    
    
       [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult LoadAdministrators()
        {
            //var settings = Properties.Settings.Default;
            var formData = HttpContext.Request.Form;
    
            using (var ctx = new OldCastle001())
            {
                using (var db = new Database("sqlserver", ctx.Database.Connection))
                {
                    var response = new Editor(db, "administrator", "administratorsid")
                        .Model<administrator>()
                        .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();
    
                    return Json(response, JsonRequestBehavior.AllowGet);
                }
            }
        }
    
  • Godrules500Godrules500 Posts: 25Questions: 13Answers: 0
    edited September 2016

    I think you can set it as resolve. I had to get the libraries from both editor and datatables downloads to have all of the correct files. Then I had to add buttons and select to perform the way I wanted it to.

  • allanallan Posts: 63,352Questions: 1Answers: 10,443 Site admin

    Good to hear you have it working.

    To answer this specific question for anyone else reading:

    Do I call the controller method that contains "new Editor()" code via ajax

    Yes. The controller is invoked when DataTables and / or Editor call it via Ajax.

    Allan

This discussion has been closed.