getting error "No parameterless constructor defined for type ..." when entering edit mode on viewmodel class without defaut empty constructor

when using a viewmodel to bind to grid, the view model MUST have a parameterless contrustrutor?
can existing viewmodel on the row edit to be used directly for edit template instead creating a new viewmodel? sometime the viewmodel has reference and complex logic so can't be fully intilized from parameter less constructor. 

@page "/datagrid-features"
@using Syncfusion.Blazor.Grids
<h2>DataGrid</h2>
<br/>
<div id="ControlRegion">
    <SfGrid ID="Grid" DataSource="@Orders" @ref="Grid"
            AllowReordering="true" AllowResizing="true" AllowSorting="true" AllowFiltering="true"
            Height="500" Width="auto" EnableVirtualization="true">
        <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Menu"></GridFilterSettings>
        <GridPageSettings PageSize="20"></GridPageSettings>
        <GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
        <GridColumns>
            <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
            <GridColumn HeaderText="Sub items">
                <Template>
                    @{
                        var vm = context as Order;
                        for (int i = 1; i <= vm.LineitemSize; i++)
                        {
                            <div>lineitem: @i</div>
                        }
                    }
                </Template>
            </GridColumn>
            <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120"></GridColumn>
            <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn>
            <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120">
                <EditTemplate>
                    @{
                        var vm = context as Order;
                        <input type="text" @bind-value="@vm.Freight" />
                    }
                </EditTemplate>
            </GridColumn>
            <GridColumn Field=@nameof(Order.FreightPct) HeaderText="Freight percent" Width="150"></GridColumn>
            <GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" Width="150"></GridColumn>
        </GridColumns>
    </SfGrid>
</div>
<br/>

<br/>
<style>
    .ulstyle {
        margin: 0px;
        padding-left: 20px;
        display: inline-block;
    }
    .list {
    float: left;
    line-height: 20px;
    margin: 10px;
    min-width: 200px;
    }
</style>
@code{
    public List<Order> Orders { get; set; }
    SfGrid<Order> Grid;
    protected override void OnInitialized()
    {
        Orders = Enumerable.Range(1, 8000).Select(x => new Order(x)
        {
            LineitemSize = new Random().Next(1,4),
            CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
            Freight = 2.1 * x,
            OrderDate = DateTime.Now.AddDays(-x),
            ShipCountry = (new string[] { "USA", "UK", "CHINA", "RUSSIA", "INDIA" })[new Random().Next(5)]
        }).ToList();
    }

    public class Order
    {
        public Order(int id)
        {
            OrderID = id;
        }
        public int? OrderID { get; set; }
        public int LineitemSize { get; set; }
        public string CustomerID { get; set; }
        public DateTime? OrderDate { get; set; }
        public double? Freight { get; set; }
        public string ShipCountry { get; set; }
        public double? FreightPct => Freight / 100;
    }
}

3 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team July 9, 2020 10:55 AM UTC

Hi Wei, 

Greetings from Syncfusion support. 

As the Grid uses Activator.CreateInstance<TItem>();  to generate a new item when an Insert operation is invoked. So it is a must to have Parameter less constructor defined for the Grid Model.  

So we suggest you to define parameter less constructor in your model, to overcome the problem you are facing. We will also include this in our release notes and this will be refreshed online. 

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 



WM wei ma July 9, 2020 02:29 PM UTC

thanks for the reply, this is a big limitation for no event hook to override how viewmodel is created. 
imagine if DI is configured for any new instance with resolving all the dependencies of a view model and Grid can't use it for edit or create . 




RS Renjith Singh Rajendran Syncfusion Team July 10, 2020 01:08 PM UTC

Hi Wei, 

Thanks for your update. 

We have considered this as a feature improvement and logged an improvement task “Need to get new record object using ActionBegin event” for this requirement. We have planned to implement this and include this feature improvement in our upcoming release which is expected to be rolled out by the end of July 2020. 
 
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.  

Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran. 



Marked as answer
Loader.
Up arrow icon