Update controller action not getting called

11 posts, 0 answers
  1. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 08 Mar 2019 Link to this post

    I've got a grid with InCell editing and I have only the Add and Save buttons in the header. The grid is in an iframe. The trouble I am having is when I click the batch save button the controller action in the Datasource is not getting called.

    <div>
        @(Html.Kendo().Window()
                .Name("timecard")
                .Modal(true)
                .Actions(actions => actions.Close())
                .Draggable(false)
                .LoadContentFrom("Timecard")
                .Events(events => events
                    .Close("timecard_OnClose")
                    .Refresh("timecard_OnIframeLoaded")
                )
                .Iframe(true)
                .Width(1700)
                .Height(800)
                .Visible(false)
                .Deferred(true)
        )
    </div>
    function timecard_OnIframeLoaded(e)
    {
        $.ajax({
            url: '@Url.Action("Timecard_Load", "Timecard")',
            type: "POST",
            datatype: "json",
            data: { id: employee_key, weekEnding: week_ending},
            success: timecard_LoadTimecardSuccess
        });
    }

     

    <div id="employeeTimecard">
        @(Html.Kendo().Grid<Timecard.Models.TimecardViewModel>()
                                .Name("timecard")
                                .ToolBar(toolbar =>
                                {
                                    toolbar.Create().Text("ADD").HtmlAttributes(new { title = "Add employee" });
                                    toolbar.Save().Text("SAVE");
                                })
                                .Editable(editable => editable.Mode(GridEditMode.InCell))
                                .Columns(columns =>
                                {
                                    columns.Bound(p => p.Job).Filterable(false).Sortable(false).Width(115).EditorTemplateName("_InCellAutoCompleteEditor").Title("Job");
                                    columns.Bound(p => p.Task).Filterable(false).Sortable(false).Width(50);
                                    columns.Bound(p => p.TaskName).Filterable(false).Sortable(false).Width(150);
                                    columns.Bound(p => p.SubTask).Filterable(false).Sortable(false).Width(75);
                                    columns.Bound(p => p.SubTaskCompDate).Filterable(false).Sortable(false).Width(75);
                                    columns.Bound(p => p.TravelPay).Filterable(false).Sortable(false).Width(75).Title("Travel Pay (Total)");
                                    columns.Bound(p => p.SpecialPayRate).Filterable(false).Sortable(false).Width(75);
                                    columns.Bound(p => p.Comment).Filterable(false).Sortable(false).Width(150);
                                    columns.Bound(p => p.MonST).Filterable(false).Sortable(false).Format("{0:n1}").Title("Mon ST").Width(40);
                                    columns.Bound(p => p.MonOT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.MonDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.TueST).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.TueOT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.TueDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.WedST).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.WedOT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.WedDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.ThuST).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.ThuOT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.ThuDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.FriST).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.FriOT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.FriDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.SatST).Filterable(false).Sortable(false).Hidden(true).Width(40);
                                    columns.Bound(p => p.SatOT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.SatDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Bound(p => p.SunST).Filterable(false).Sortable(false).Hidden(true).Width(40);
                                    columns.Bound(p => p.SunOT).Filterable(false).Sortable(false).Hidden(true).Width(40);
                                    columns.Bound(p => p.SunDT).Filterable(false).Sortable(false).Width(40);
                                    columns.Command(command =>
                                    {
                                        command.Destroy().HtmlAttributes(new { title = "Delete highlighted employee"});
                                    }).Title("Options").Width(100);
                                })
                                .Sortable()
                                .Scrollable()
                                .Filterable()
                                .HtmlAttributes(new { style = "height:650px;width:1615px;" })
                                .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .Batch(true)
                                    .PageSize(100)
                                    .Model(model => model.Id(p => p.EmployeeCode))
                                    .Update(update => update.Action("Timecard_Update", "Timecard"))
                                )
        )
    </div>

     

     

     

     

  2. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 08 Mar 2019 Link to this post

    public ActionResult Timecard_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<TimecardViewModel> timecard)
    {
        int setKey = 1;
        int monHrs = 7;
     
        using (SqlConnection conn = new SqlConnection("Server=172.16.10.25;DataBase=GeminiDevelopment;Integrated Security=SSPI"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "uspGeminiEmployeeTimecardUpdate";
                cmd.Parameters.Add("@set_key", SqlDbType.Int);
                cmd.Parameters["@set_key"].Value = setKey;
     
                cmd.Parameters.Add("@mon_set", SqlDbType.DateTime2);
                cmd.Parameters["@mon_set"].Value = monHrs;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Connection = conn;
     
                conn.Open();
     
                cmd.ExecuteReader();
            }
        }
     
        return Json(timecard.ToDataSourceResult(request, ModelState));
    }
  3. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 08 Mar 2019 Link to this post

    public ActionResult Timecard_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<TimecardViewModel> timecard)
    {
        int setKey = 1;
        int monHrs = 7;
     
        using (SqlConnection conn = new SqlConnection("Server=172.16.10.25;DataBase=GeminiDevelopment;Integrated Security=SSPI"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "uspGeminiEmployeeTimecardUpdate";
                cmd.Parameters.Add("@set_key", SqlDbType.Int);
                cmd.Parameters["@set_key"].Value = setKey;
     
                cmd.Parameters.Add("@mon_set", SqlDbType.DateTime2);
                cmd.Parameters["@mon_set"].Value = monHrs;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Connection = conn;
     
                conn.Open();
     
                cmd.ExecuteReader();
            }
        }
     
        return Json(timecard.ToDataSourceResult(request, ModelState));
    }
  4. David
    David avatar
    77 posts
    Member since:
    Nov 2011

    Posted 09 Mar 2019 Link to this post

    Hi Mark I got confused by this one to you need use the post markup as follows. The bit I am having trouble with as my activity is always null and I do not no why.

    [AcceptVerbs("Post")]
            public IActionResult Activity_Update([DataSourceRequest]DataSourceRequest request, ActivityLines activity,int? activityId)
            {
          
                    int ActivityLineId = (int)activityId;
                    ActivityLines nLines = _activityRepo.GetActivityLineById(activityId);
                    nLines.Description = nLines.Description + "test do i savE";
                    _activityRepo.UpdateActivityLines(nLines);                    
                return Json(new[] { nLines }.ToDataSourceResult(request, ModelState));
                
             }
  5. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 10 Mar 2019 in reply to David Link to this post

    David, thanks for the help but adding [AcceptVerbs("Post")] did not work.
  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 13 Mar 2019 Link to this post

    Hello Mark,

    Could you please elaborate if there are any errors in the browser's console or any requests at all when you hit the "Save" button? As for the Grid's and the Action method configuration, it seems correct (after adding the AcceptVerbs). You could refer to our online demo (EditingController.cs):
    On a side note, does the Grid work correctly if you place it directly in the page, instead of rendering it in the iframe (in the Window)?

    Looking forward to your reply.


    Regards,
    Konstantin Dikov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  7. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 13 Mar 2019 in reply to Konstantin Dikov Link to this post

    There are no errors shown in my browser's (Chrome) console. I haven't yet tried to put the grid directory in the page.

    When the grid is first shown, I am populating it from the parent by adding items to the datasource by way of the window's Refresh event and making an ajax call to the controller there.

    function timecard_OnIframeLoaded(e)
        {
            $.ajax({
                url: '@Url.Action("Timecard_Read", "Timecard")',
                type: "POST",
                datatype: "json",
                data: { id: employee_key, weekEnding: week_ending},
                success: ReadTimecardSuccess
            });
        }
     
        function ReadTimecardSuccess(data)
        {
            // Parse out data into array
            var timecardData = JSON.parse(data);
            var windowElement = $("#timecard");
            var iframeElement = windowElement.children("iframe")[0];
            var iframeContentWindow = iframeElement.contentWindow;      
            var grid = iframeContentWindow.$("#timecard").data("kendoGrid");
            for (i = 0; i < timecardData.length; i++)
            {
                var dataSource = grid.dataSource;
                dataSource.add({
                    Job: timecardData[i].ProjectName
                    ,Task: timecardData[i].TaskNumber
                    ,TaskName: timecardData[i].TaskName
                    ,MonST: timecardData[i].MonST
                    ,MonOT: timecardData[i].MonOT
                    ,MonDT: timecardData[i].MonDT
                    ,TueST: timecardData[i].TueST
                    ,TueOT: timecardData[i].TueOT
                    ,TueDT: timecardData[i].TueDT
                    ,WedST: timecardData[i].WedST
                    ,WedOT: timecardData[i].WedOT
                    ,WedDT: timecardData[i].WedDT
                    ,ThuST: timecardData[i].ThuST
                    ,ThuOT: timecardData[i].ThuOT
                    ,ThuDT: timecardData[i].ThuDT
                    ,FriST: timecardData[i].FriST
                    ,FriOT: timecardData[i].FriOT
                    ,FriDT: timecardData[i].FriDT
                    ,SatOT: timecardData[i].SatST
                    ,SatDT: timecardData[i].SatOT
                    ,SunDT: timecardData[i].SunDT });
                dataSource.sync();
           }

     

  8. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 13 Mar 2019 Link to this post

    If I create a custom toolbar button and use an onclick in HtmlAttributes, the JavaScript function "Test_Click()" is called.

    toolbar.Custom().Text("CLICK").HtmlAttributes(new { onclick="Test_Click();"});

     

  9. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 13 Mar 2019 Link to this post

    If I create a custom toolbar button and use HtmlAttributes to set an onclick event, the JavaScript function "Test_Click()" is called.

    toolbar.Custom().Text("CLICK").HtmlAttributes(new { onclick="Test_Click();"});

     

     

  10. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 18 Mar 2019 Link to this post

    Hi Mark,

    I have noticed that you are manually adding items to the DataSource, but since there is no "Create" action method defined in the Grid's DataSource, those records will never be added to the database and no ID will be returned for them, which means that when you try to edit them, the DataSource will still handle them as new and will try to call the "Create" method and not the "Update". With that in mind, please define the "Create" as well and return the unique ID's, along with the model, for each record.

    Let me know if this resolves the issue that you are facing.


    Regards,
    Konstantin Dikov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  11. Mark
    Mark avatar
    30 posts
    Member since:
    Jan 2019

    Posted 19 Mar 2019 in reply to Konstantin Dikov Link to this post

    Thank you. I changed how I was adding and updating data to use the DataSource methods. It works now.
Back to Top