What is the standard setup for a grid in a tag helper form

1 Answer 55 Views
Form Grid
Noah
Top achievements
Rank 1
Iron
Noah asked on 29 Oct 2024, 03:21 PM

I was able to add a grid to the form, but when clicking save the grid data does not submit with the rest of the form.

I was wondering if there are any articles on this situation or if I have to write custom JavaScript to handle it in on-submit of the form.

this is roughly my setup.

<kendo-form name="bid-form" form-data="@Model" layout="grid" asp-controller="" asp-action="" method="post">

... other items

<form-item field="@(nameof(Bid.ObjectList))" editor-handler="gridAsEditor"></form-item>
</form-items>
</kendo-form>

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 31 Oct 2024, 11:05 AM

Hello Noah,

Indeed, you can submit the Grid's data thorough the form, as discussed in the following forum thread:

https://www.telerik.com/forums/grid-within-form#5867742

Here is the same example but with TagHelper Grid:

<kendo-grid name="myGrid">
  <columns>
    <column field="Field1" template="#=Field1# <input type='hidden' name='myGrid[#= index(data)#].Field1' value='#= Field1#' />"></column>
    <column field="Field2" template="#=Field2# <input type='hidden' name='myGrid[#= index(data)#].Field2' value='#= Field2#' />"></column>
                      ...
  </columns>
  ...
 </kendo-grid>

<script>
    function index(dataItem) {
        var data = $("#myGrid").data("kendoGrid").dataSource.data(); // get a reference to the Grid by using its "id" attribute
        return data.indexOf(dataItem);
    }
</script>

As a result, the hidden inputs within the Grid's column templates will be submitted through the form. It is important to ensure that the "name" of the Grid matches the name of the Model property that holds the Grid's data:

//MyModel.cs --> the Model data binds to the Form
public  List<OrderViewModel> myGrid { get; set; }

I hope this example will help you to submit the Grid's data. If you have any questions, please let me know.

Regards,
Mihaela
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Form Grid
Asked by
Noah
Top achievements
Rank 1
Iron
Answers by
Mihaela
Telerik team
Share this question
or