Search code examples
c#asp.net-mvclinqknockout.jsodata

Error in creating a valid action to bind to a collection


I am developing an Asp.Net MVC application which deals with CRUD operation on a SQL database using Odata and LINQ. I am trying to delete all the data from an SQL table in a database by creating an Action on the controller as follows

Server side code

 private WhiteBoardAppContext db = new WhiteBoardAppContext();


public override HttpResponseMessage HandleUnmappedRequest(System.Web.Http.OData.Routing.ODataPath odataPath)
        {
            HttpResponseMessage emptyMSG = new HttpResponseMessage();
            switch (odataPath.Segments[2].ToString()) // kick out if the value is the same
            {


                case "DeleteSegment":
                                    string Product = odataPath.Segments[1].ToString();
                                    byte[] param = new byte[Product.Length / 2];
                                    for (int i = 0; i < param.Length; i++)
                                    {
                                        param[i] = Convert.ToByte(Product.Substring(i * 2, 2), 16);
                                    }
                                    Product = System.Text.Encoding.ASCII.GetString(param);
                                    using (db)
                                    {
                                        var SegmentToDelete = from c in db.tblItems
                                                                    where c.Product == Product
                                                                    select c;
                                        foreach (tblItem cr in SegmentToDelete)
                                        {
                                            db.tblItems.Remove(cr);

                                        }
                                        db.SaveChanges();
                                    }

                                    return emptyMSG;

                case "DeleteAll":

                                    using (db)
                                    {
                                        var itemsToClear = from c in db.tblItems
                                                                   select c;
                                        foreach (tblItem cr in itemsToClear)
                                        {
                                            db.tblItems.Remove(cr);

                                        }
                                        db.SaveChanges();
                                    }

                                    return emptyMSG;


default:
                    return base.HandleUnmappedRequest(odataPath);
            }
        }

Client code using Knockout JS

self.deleteAll = function () {
        var conf = confirm("Are you sure you want to delete all?");
        if (conf == true) {
            $.ajax({
                url: '/odata/Items/DeleteAll'
            });

        }
    }

When I assigned to a button click, it didn't work as I want instead it through me an error as follows

GET http://localhost:57044/odata/Canadiancrudes/DeleteAll 500 (Internal Server Error) 
Invalid action detected. 'DeleteAll' is not an action that can bind to 'Collection([WhiteBoardApp.Models.Item Nullable=False])'.

Solution

  • Simple Error, Fixed it myself as follows

    self.deleteAll = function (item) {
            var conf = confirm("Are you sure you want to delete all?");
            if (conf == true) {
                $.ajax({
                    url: '/odata/Items('+item.id+')/DeleteAll'
                });
    
            }
        }