New added row not showing correctly on asp.net mvc

New added row not showing correctly on asp.net mvc

samsinfieldsamsinfield Posts: 3Questions: 1Answers: 0
edited January 2016 in Free community support

I'm using asp.net MVC and sqlserver
I'm currently evaluating the editor prior to purchase. Its great apart from one problem which I just can't figure out.
I'm having exactly the same problem as
https://datatables.net/forums/discussion/28999/new-added-row-not-showing-instead-showing-first-record-twice and
https://datatables.net/forums/discussion/28886/creating-new-row-returns-wrong-row-by-using-sqlite-as-database

I went onto the generator to generate the code and downloaded the package, integrated it with my database and it worked perfectly,
I copied the code into my project but when adding a new row, it inserts it into the database fine but then the JSON returns the first row. The edit and delete work perfectly. Its just the create. I've stepped through the code and up until the editor process, the fields look exactly the same as the generator code apart from some authentication fields

Answers

  • samsinfieldsamsinfield Posts: 3Questions: 1Answers: 0
    edited January 2016

    My controller:

    public class JSONController : ApiController
        {
            // GET: JSON
            [Route("api/JSONs/GetQuestions2")]
            [HttpGet]
            [HttpPost]
            public IHttpActionResult GetQuestions2()
            {
                string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                var request = HttpContext.Current.Request;
    
                using (var db = new DataTables.Database("sqlserver", connString))
                {
                    var response = new Editor(db, "Questions", "Id")
                         .Model<QuestionsModel>()
                         .Process(request)
                         .Data();
    
                    return Json(response);
                }
            }
        }
    
  • samsinfieldsamsinfield Posts: 3Questions: 1Answers: 0

    My html and js:

    <table cellpadding="0" cellspacing="0" border="0" class="display" id="Questions" width="100%">
        <thead>
            <tr>
                <th>Number</th>
                <th>FieldName</th>
                <th>Text</th>
                <th>IsRequired</th>
                <th>Answers</th>
                <th>QuestionTypeId</th>
                <th>EventId</th>
            </tr>
        </thead>
    </table>
    <script>
        (function ($) {
    
            $(document).ready(function () {
                var editor = new $.fn.dataTable.Editor({
                    ajax: {
                        "url": '@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))api/JSON/GetQuestions2'
                    },
                    table: '#Questions',
                    fields: [
                        {
                            "label": "Number",
                            "name": "number"
                        },
                        {
                            "label": "FieldName",
                            "name": "fieldname"
                        },
                        {
                            "label": "Text",
                            "name": "text"
                        },
                        {
                            "label": "IsRequired",
                            "name": "isrequired",
                            "type": "select",
                            "options": [
                                ""
                            ]
                        },
                        {
                            "label": "Answers",
                            "name": "answers"
                        },
                        {
                            "label": "QuestionTypeId",
                            "name": "questiontypeid"
                        },
                        {
                            "label": "EventId",
                            "name": "eventid"
                        }
                    ]
                });
    
                var table = $('#Questions').DataTable({
                    dom: 'Bfrtip',
                    ajax: {
                        "url": "@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))api/JSON/GetQuestions2"
                    },
                    columns: [
                        {
                            "data": "number"
                        },
                        {
                            "data": "fieldname"
                        },
                        {
                            "data": "text"
                        },
                        {
                            "data": "isrequired"
                        },
                        {
                            "data": "answers"
                        },
                        {
                            "data": "questiontypeid"
                        },
                        {
                            "data": "eventid"
                        }
                    ],
                    select: true,
                    lengthChange: false,
                    buttons: [
                        { extend: 'create', editor: editor },
                        { extend: 'edit', editor: editor },
                        { extend: 'remove', editor: editor }
                    ],
                    
                }).makeEditable({
                    fnOnAdded: function () {
                        oTable.fnDraw();
                    }
                });
            });
    
        }(jQuery));
    </script>
    
This discussion has been closed.