How to pass parameter into editor webapi in asp.net framework for where condition

How to pass parameter into editor webapi in asp.net framework for where condition

KingletKinglet Posts: 1Questions: 1Answers: 0

I try to pass a parameter for where condition on asp.net webapi, but it can not execute and report error:

DataTables warning: table id=example - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Here are my code modified from api/staff

C# Code:

public class StaffController : ApiController
{
    [Route("api/staff")]
    [HttpGet]
    [HttpPost]
    public IHttpActionResult Staff([FromUri]string office)
    {
        var request = HttpContext.Current.Request;
        var settings = Properties.Settings.Default;

        using (var db = new Database(settings.DbType, settings.DbConnection))
        {
            var response = new Editor(db, "datatables_demo")
                .Model<StaffModel>()
                .Field(new Field("first_name")
                    .Validator(Validation.NotEmpty())
                )
                .Field(new Field("last_name"))
                .Field(new Field("extn")
                    .Validator(Validation.Numeric())
                )
                .Field(new Field("age")
                    .Validator(Validation.Numeric())
                    .SetFormatter(Format.IfEmpty(null))
                )
                .Field(new Field("salary")
                    .Validator(Validation.Numeric())
                    .SetFormatter(Format.IfEmpty(null))
                )
                .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))
                )
                .Where("office", office, "=")
                .Process(request)
                .Data();

            return Json(response);
        }
    }
}

Java Code:

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function () {
    editSimple("Tokyo");
} );

function editSimple(office) {
        editor = new $.fn.dataTable.Editor({
            ajax: "/api/staff",
            table: "#example",
            fields: [{
                label: "First name:",
                name: "first_name"
            }, {
                label: "Last name:",
                name: "last_name"
            }, {
                label: "Position:",
                name: "position"
            }, {
                label: "Office:",
                name: "office"
            }, {
                label: "Extension:",
                name: "extn"
            }, {
                label: "Start date:",
                name: "start_date",
                type: "datetime"
            }, {
                label: "Salary:",
                name: "salary"
            }
            ]
        });

$('#example').DataTable({
    dom: "Bfrtip",
    ajax: {
        url: "/api/staff",
        type: "POST",
        data: {'office': office}
    },
            columns: [
                {
                    data: null, render: function (data, type, row) {
                        // Combine the first and last names into a single table field
                        return data.first_name + ' ' + data.last_name;
                    }
                },
                { data: "position" },
                { data: "office" },
                { data: "extn" },
                { data: "start_date" },
                { data: "salary", render: $.fn.dataTable.render.number(',', '.', 0, '$') }
            ],
            select: true,
            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit", editor: editor },
                { extend: "remove", editor: editor }
            ]
        });
}

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin

    What is the response from the server? If you follow the tech note link in the error message, it will show you how to get that if you aren't sure.

    Allan

Sign In or Register to comment.