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
close icon

Delete grid line using multiple primary key

Hi

I am trying to delete grid line where I have four primary key 

@(Html.EJ().Grid<emp.Models.UseDocLineModel>("FlatGrid")
                 .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).UpdateURL("Update").InsertURL("NormalInsert").RemoveURL("DeleteLine").Adaptor(AdaptorType.RemoteSaveAdaptor))
            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing();  })
         
            .ToolbarSettings(toolbar =>
            {
                toolbar.ShowToolbar().ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems.Add);
                    items.AddTool(ToolBarItems.Edit);
                    items.AddTool(ToolBarItems.Delete);
                    items.AddTool(ToolBarItems.Update);
                    items.AddTool(ToolBarItems.Cancel);
                });
            })
.Columns(col =>
                {
                    col.Field("org_id").HeaderText("Organization").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width(70).Visible(false).Add();
                    col.Field("document_code").HeaderText("Document Code").IsPrimaryKey(true).TextAlign(TextAlign.Left).Visible(false).Width(70).Add();
                    col.Field("document_no").HeaderText("Document No").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width(70).Visible(false).Add();
                    col.Field("line_no").HeaderText("line No").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width(70).Visible(false).Add();
                     .............

In the controller , here is my code for DeleteLine

public ActionResult DeleteBudgetLine(int key, string key1, int key3, int key4)
        {
.................
         return Json(docLines, JsonRequestBehavior.AllowGet);
        }


8 Replies

SS Shanzida Sharaf February 24, 2016 07:35 AM UTC

sorry for incomplete thread

Hi

I am trying to delete grid line where I have four primary key 

@(Html.EJ().Grid<emp.Models.UseDocLineModel>("FlatGrid")
                 .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).UpdateURL("Update").InsertURL("NormalInsert").RemoveURL("DeleteLine").Adaptor(AdaptorType.RemoteSaveAdaptor))
            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing();  })
         
            .ToolbarSettings(toolbar =>
            {
                toolbar.ShowToolbar().ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems.Add);
                    items.AddTool(ToolBarItems.Edit);
                    items.AddTool(ToolBarItems.Delete);
                    items.AddTool(ToolBarItems.Update);
                    items.AddTool(ToolBarItems.Cancel);
                });
            })
.Columns(col =>
                {
                    col.Field("org_id").HeaderText("Organization").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width(70).Visible(false).Add();
                    col.Field("document_code").HeaderText("Document Code").IsPrimaryKey(true).TextAlign(TextAlign.Left).Visible(false).Width(70).Add();
                    col.Field("document_no").HeaderText("Document No").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width(70).Visible(false).Add();
                    col.Field("line_no").HeaderText("line No").IsPrimaryKey(true).TextAlign(TextAlign.Left).Width(70).Visible(false).Add();
                     .............

In the controller , here is my code for DeleteLine

public ActionResult DeleteBudgetLine(int key, string key1, int key3, int key4)
        {
.................
         return Json(docLines, JsonRequestBehavior.AllowGet);
        }

I am only getting  value for key not for the other. But I need to get all values to get the data. How can I get the primary key value for delete case


GV Gowthami V Syncfusion Team February 25, 2016 11:17 AM UTC

Hi Shanzida,

Thanks for using Syncfusion products.

We can achieve your requirement “get all primary key values to get the data” 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. 

@(Html.EJ().Grid<emp.Models.UseDocLineModel>("FlatGrid")

                 .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).UpdateURL("Update").InsertURL("NormalInsert").RemoveURL("DeleteLine").Adaptor(AdaptorType.RemoteSaveAdaptor))

    . . . .

    . . . .

    .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.document_code, "additional_key1": args.data.document_no, "additional_key2": args.data.line_no });


            }

        }

        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 DeleteLine(int key //primarykey value)

    {


    NORTHWNDEntities db = new NORTHWNDEntities();

    //getting additional key values

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

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

    int obj2 = Int32.Parse(Request.Headers.GetValues("additional_key2")[0]); //key3

    . . . .

    . . . .

   //code for delete the record

    return Json(docLines, JsonRequestBehavior.AllowGet);
    }


We have already discussed about “how to pass custom headers to server through datamanager” in the below link,

https://www.syncfusion.com/kb/5963/how-to-send-custom-headers-to-server-using-datamanager
 
Regards,

Gowthami V.


SS Shanzida Sharaf February 25, 2016 11:47 AM UTC

Thanks for your quick reply. I will try and let you know my feedback.


GV Gowthami V Syncfusion Team February 26, 2016 03:57 AM UTC

Hi Shanzida,
 
Try the solution and get back to us if you need further assistance.
 
Regards,
 
Gowthami V.


SS Shanzida Sharaf March 1, 2016 09:29 AM UTC

Hi Gowthami,

I have tried this solution and it works for me. 

Just one more query for you. Could you please let me know in the delete method which key return e.g. Delete ( int key) Is this is the first primary key of my model? 

Thanks for your help.

kind regards,
Shanzida


GV Gowthami V Syncfusion Team March 2, 2016 08:37 AM UTC

Hi Shanzida,

No. The last primary key column value will be passed to the Delete controller method as a key parameter.

Refer to below screenshot (OrderID, EmployeeID and Freight fields are PrimaryKey field)

Grid



Delete method



Regards,

Gowthami V.


MM Mahindra Morar March 4, 2016 06:20 AM UTC

Hi, Is there an option to return a error message if the deletion fails on the server side?


GV Gowthami V Syncfusion Team March 7, 2016 08:36 AM UTC

Hi Shanzida,

We have already discussed about the topic “Displaying error message if server action failed” in the below KB document,
                                                                                                                                  
https://www.syncfusion.com/kb/5170/how-to-display-custom-error-message-on-ajax-action-failture

Regards,

Gowthami V.


Loader.
Live Chat Icon For mobile
Up arrow icon