controller Where statement not getting data

controller Where statement not getting data

montoyammontoyam Posts: 568Questions: 136Answers: 5
edited September 2020 in Editor

I must be missing something but I am not seeing it.

I have the following in the controller:

    public class SystemUsersLineItemsController : ApiController
    {
        [Route("api/SystemUsersLineItems")]
        [HttpGet]
        [HttpPost]
        public IHttpActionResult SystemUsersLineItems()
        {
            var request = HttpContext.Current.Request;
            var settings = Properties.Settings.Default;
            using (var db = new Database(settings.DbType, settings.DbConnection))
            {
                var response = new Editor(db, "SystemUsers_LineItems", "UserLineItemID")
                .Model<SystemUsersLineItemsModel>("SystemUsers_LineItems")
                .Field(new Field("SystemUsers_LineItems.SystemUserID")
                        .Options(new Options()
                                    .Table("SystemUsers")
                                    .Value("SystemUserID")
                                    .Label(new[] { "FirstName", "LastName" })
                                    .Render(row => row["LastName"] + ", " + row["FirstName"])
                        )
                    )
                .LeftJoin("SystemUsers", "SystemUsers.SystemUserID", "=", "SystemUsers_LineItems.SystemUserID")
                    .Field(new Field("SystemUsers.FirstName"))
                    .Field(new Field("SystemUsers.LastName"))

                .Where("SystemUsers_LineItems.SystemUserID", request.Form["userIDFilter"])
                .Debug(true)
                 .Process(request)
                .Data();
                return Json(response);
            }
        }
    }

and the following in the dataTable:

                ajax: {
                    url: 'api/SystemUsersLineItems',
                    data: function (d) {
                        var selected = SystemUsersTable.row({ selected: true });
                        if (selected.any()) {
                            d['userIDFilter'] = selected.data().SystemUsers.SystemUserID;
                            console.log("idFilter",selected.data().SystemUsers.SystemUserID);

                        }
                    }
                },

now, in the console.log I see:

idFilter 64027

so I know 'userIDFilter' is populated

however, I don't get the correct data and looking at the debug from the controller I see:

WHERE [SystemUsers_LineItems].[SystemUserID] IS NULL 

Also, in the the browser developer tools --> network --> header --> Query String parameters I see:

userIDFilter: 64027

So why is request.Form["userIDFilter"] returning NULL

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,382Questions: 26Answers: 4,959

    Guessing you are using ASP.Net. I don't myself so this is a guess but it looks like you need Request.Form["userIDFilter"] instead of request.Form["userIDFilter"]. Notice the capital R. See this SO thread.

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited September 2020

    no, here request (with lower case r) is from line 8 in the controller posted above.

    I went ahead and changed it so I am passing the ID number as a parameter, but I don't understand why it isn't working as posted above because I have done this many times before.

    here is what i ended up doing this time:

        public class SystemUsersLineItemsController : ApiController
        {
            [Route("api/SystemUsersLineItems")]
            [HttpGet]
            [HttpPost]
            public IHttpActionResult SystemUsersLineItems(int systemUserID)
    

    and called it like this:

                var SystemUsersLineItemsEditor = new $.fn.dataTable.Editor({
                    ajax: 'api/SystemUsersLineItems?systemUserID=' + rowID,
    
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    edited September 2020 Answer ✓

    request.Form["userIDFilter"] is populated when your POST the data to the server. So you need to add type: 'POST' to your ajax object.

    Edit By default DataTables sends a GET request.

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    aaah, yes. thank you.

This discussion has been closed.