We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Delete Method with Composite Key

I have a a table with a Composite Key (int, int, string).  I have working code for a standard table (1st below) modified to work with the Composite key (2nd below). However when I press the delete icon nothing happens. Code breaks in the method don't even register that the method is being called. Is the something in the grid a can modify or do I need a new approach? Thanks.

Working code (with standard Primary Key)
        public ActionResult Delete(int key)
        {
            Result result = db.Result.Find(key);
            db.Result.Remove(result);
            db.SaveChanges();
            return Json(GetResults(), JsonRequestBehavior.AllowGet);
        }

Attempted code (with Composite Key)
        public ActionResult Delete(int key0, int key1, string key2)
        {
            Result result = db.Result.Find(key0, key1, key2);
            db.Result.Remove(result);
            db.SaveChanges();
            return Json(GetResults(), JsonRequestBehavior.AllowGet);
        }

3 Replies

GV Gowthami V Syncfusion Team January 5, 2016 12:20 PM UTC

Hi Todd,

Thanks for using Syncfusion products.

We can achieve your requirement by passing the additional key values using the Headers of the ejDataManager. While performing delete action in the actionBegin event, add the required number of array of objects into Headers and retrieve them using the Request Object of the HttpRequestMessage. 

Refer to the below code example,

@(Html.EJ().Grid<OrderTable>("Grid")

        .Datasource(ds => ds.URL("GetOrderData")

                            . . . .

                            .RemoveURL("PerformDelete")

                            .Adaptor(AdaptorType.UrlAdaptor)
                            )
. . . .
. . . .

.ClientSideEvents(eve => { eve.ActionBegin("begin").ActionComplete("complete");})

                )

<script type="text/javascript">

    function begin(args) {

        if (args.requestType == 'delete') {

            args.model.dataSource.dataSource.headers = [];

            args.model.dataSource.dataSource.headers.push({ "additional_key": args.data.EmployeeID,"additional_key1":args.data.CustomerID });

          

        }

    }

    function complete(args) {

        if (args.requestType == 'delete') args.model.dataSource.dataSource.headers = [];//to avoid headers value to be interfered with other actions, emptied the Headers 

    }
    </script>

//Perform delete

        public ActionResult PerformDelete(int key //primarykey value)

        {


            NORTHWNDEntities db = new NORTHWNDEntities();

            //getting additional key values

            int obj = Int32.Parse( Request.Headers.GetValues("additional_key")[0]); //key1

            string obj1 = Request.Headers.GetValues("additional_key1")[0]; //key2

            . . . .

            . . . .

            db.SaveChanges();

            return RedirectToAction("GetOrderData");

        }


Refer to the below screenshot for more clarification,




Regards,

Gowthami V.


TM Todd M January 5, 2016 03:18 PM UTC

Thank you. I got it working with a slight modification. I noticed that the "delete" method would only be called IF the value was a pure numeric string (e.g. 1000), if the value was 11B then the method wouldn't even be called.  I changed the method argument from int key to string key.  Works great.


RU Ragavee U S Syncfusion Team January 6, 2016 04:24 AM UTC

Hi Todd,

Thanks for your update.

We are happy that your requirement is achieved.

Regards,
Ragavee U S

Loader.
Live Chat Icon For mobile
Up arrow icon