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

Compliex object binding in edit mode

Hi!
I'm trying to show and edit complex object in Grid.

Object is: 
public class WorkoutTaskModel
    {
        public int Id { get; set; }
        public ExerciseModel Exercise { get; set; }
        public int? Approaches { get; set; }
        public int? Repetitions{ get; set; }
}

 public class ExerciseModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

How to to show Exercise name in Grid, but use dropdown filled with List<ExerciseModel>  in Edit mode?
<EjsGrid DataSource="Workout.WorkoutTasks" ID="Grid" AllowPaging="true" EnableHover="true"  Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update"})">
            <GridEditSettings AllowAdding="true" AllowDeleting="true" ShowDeleteConfirmDialog="true" AllowEditing="true" AllowEditOnDblClick="true" Mode="EditMode.Normal">
            </GridEditSettings>
            <GridColumns>
                <GridColumn Field="Exercise.Name">
                    <EditTemplate>
                        <EjsDropDownList ID="Exercise" Value="@((context as WorkoutTaskModel).ExerciseId)" DataSource="@ExercisesList" FloatLabelType="FloatLabelType.Always" Placeholder="Exercise">
                            <DropDownListFieldSettings Value="Id" Text="Name"></DropDownListFieldSettings>
                        </EjsDropDownList>
                    </EditTemplate>
                </GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.Approaches)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.Repetitions)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.ExecutionTime)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.ColorCode)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.RestTime)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.ExerciseLoad)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.Order)"></GridColumn>
                <GridColumn Field="@nameof(WorkoutTaskModel.Id)" IsIdentity="true" Visible="false"></GridColumn>
            </GridColumns>
        </EjsGrid>

In this case View mode is ok, Name is shown. But in editing mode dropdown not working.


1 Reply

VN Vignesh Natarajan Syncfusion Team January 23, 2020 03:14 PM UTC

Hi Aleksandrs,  
  
Thanks for contacting Syncfusion support.  
  
Query:” How to to show Exercise name in Grid, but use dropdown filled with List<ExerciseModel>  in Edit mode? 
  
We have achieved your requirement using Column Template feature and Edit Template feature of EjsGrid.  From your query we understand that you want to display a text of complex object in Grid and while editing, you want to render DropDownList with text and value pair.  
  
Using ColumnTemplate feature we have displayed the Text in GridColumn and using OnActionBegin event, we have saved the changed value into datasource. Refer the below code example. 
  
<EjsGrid DataSource="@Orders" AllowPaging="true" EnableHover="true" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update"})"> 
    <GridEvents OnActionBegin="OnBegin" TValue="Order"></GridEvents> 
. . . . . . .. .  
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        @*Bind integer value to grid COlumn*@ 
        <GridColumn Field="Employee.EmployeeID" HeaderText="Employee Name" Width="150"> 
            <Template> 
                @{ 
                    var val = (context as Order).Employee.FirstName;  // display its corresponding  text with column template                   
                    <span>@val</span> 
                } 
            </Template> 
            <EditTemplate> 
                <EjsDropDownList @ref="Ddl" Value="@((context as Order).Employee.EmployeeID)" DataSource="@Employees" Placeholder="Exercise"> 
                    <DropDownListFieldSettings Value="EmployeeID" Text="FirstName"></DropDownListFieldSettings> 
                </EjsDropDownList> 
            </EditTemplate> 
        </GridColumn> 
. . . . . .  
    </GridColumns> 
</EjsGrid> 
  
@code{ 
    EjsDropDownList<int?, EmployeeData> Ddl { get; set; } 
    public List<Order> Orders { get; set; } 
    public static List<EmployeeData> Employees { get; set; } 
    public void OnBegin(ActionEventArgs<Order> Args) 
    { 
        //Save the change in datasource on clicking the udpate button  
        if(Args.RequestType == Syncfusion.EJ2.Blazor.Grids.Action.Save) 
        { 
            Args.Data.Employee.EmployeeID = Ddl.Value; 
            Args.Data.Employee.FirstName = Employees.Where(f => f.EmployeeID == Ddl.Value).FirstOrDefault().FirstName; 
        } 
    } 
    protected override void OnInitialized() 
    { 
         .. . . ..  . . . 
    } 
  
    
For your convenience we have prepared a sample which can be downloaded from below  
  
  
Refer our UG documentation for your reference 
  
  
  
Please get back to us if you have further queries.  
  
Regards, 
Vignesh Natarajan. 


Loader.
Live Chat Icon For mobile
Up arrow icon