New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Creating Action Without Update Button in a Grid Command Column

Environment

ProductGrid for Progress® Telerik® UI for ASP.NET MVC

Description

I have a grid where I only need Create, Read and Destroy actions. However, if I have no command.Edit(); in the c.Command, then there is no way to create a record.

If I add the command.Edit();, then it throws an error, saying I need to hook up an Update method.

How do I fix this?

Solution

It is possible to achieve the desired behavior by providing a custom save button and making it visible only when the edited model is new.

Here is one way of achieving the desired result:

  1. Add a custom command button. You may use the IconClass() to get it to look like the built-in save buttons:

    Razor
        columns.Command(c =>
        {
            c.Destroy();
            c.Custom("Save").Click("saveRow").IconClass("k-icon k-i-check").Visible     ("shouldBeVisible");
        });
  2. Add the click handler which will save the row and trigger the create action:

    Razor
        function saveRow(e) {
            e.preventDefault();
            // e.target is the DOM element representing the button
            var tr = $(e.target).closest("tr");
            this.saveRow();
        }
  3. Set the visibility of the custom button to true only for new models:

    Razor
        function shouldBeVisible(e) {       
            return e.isNew();
        }

More ASP.NET Core Grid Resources

See Also