Hi,
I am encountering an issue with a enum type in a grid edit dialog.
In attached fake code The field "AuthoriedUser.Role" does not show at all in edit dialog. How to fix this?
I looked over this thread https://www.syncfusion.com/forums/153426/how-to-grid-editdialog-template-with-enum-dropdown, which is a similar question - but the solution seems more complicated than I can imagine. With the newer version, do you have simpler/straightforward solution - make a enum type works in a dropdown just like other non-enum type ?
Thanks,
in attachment the testing project.
Many thanks,
Yi
And the same code https://www.syncfusion.com/forums/153426/how-to-grid-editdialog-template-with-enum-dropdown does not work under
<PackageReference Include="Syncfusion.Blazor" Version="19.4.0.48" />
The following code makes things even more confusing. Under:
<PackageReference Include="Syncfusion.Blazor" Version="19.4.0.48" />
I have impression that `dropDownReadOk` makes dropdown reading ok but not writing; and `dropDownWriteOk` makes dropdown writing ok but not reading.
Please suggest a correct way to make enum works with in grid edit.
Thanks
Yi
@page "/"
@using Syncfusion.Blazor.DropDowns;
@using Syncfusion.Blazor.Inputs;
@using EditMode = Syncfusion.Blazor.Grids.EditMode
<SfGrid Toolbar="@(new List<string>() { "Add", "Edit", "Update", "Cancel" })" DataSource="@Users">
<GridEvents OnActionBegin="OnActionBegin" TValue="AuthorizedUser"></GridEvents>
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Dialog">
<Template>
@{
var user = context as AuthorizedUser;
<div>
<div class="form-row">
<div class="form-group col-md-6">
<label class="e-float-text e-label-top">Id</label>
<SfTextBox ID="id" Value="@(user.Id)" @ref="userId" Enabled="true"></SfTextBox>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<SfDropDownList @ref="dropDownReadOk" FloatLabelType="Syncfusion.Blazor.Inputs.FloatLabelType.Always"
DataSource="@dropDownReadValues"
Value="@(user.Role.ToString())"
TValue="string" TItem="string">
</SfDropDownList>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<SfDropDownList @ref="dropDownWriteOk" TItem="RoleFields" TValue="string" PopupHeight="230px"
@bind-Value="@(user.RoleAsText)" @* // ===> the value is not taken by the control !!!! *@
DataSource="@roles">
<DropDownListEvents TItem="RoleFields" TValue="string"/>
<DropDownListFieldSettings Text="Text" Value="ID"/>
</SfDropDownList>
</div>
</div>
</div>
}
</Template>
</GridEditSettings>
<GridColumns>
<GridColumn Field="@nameof(AuthorizedUser.Id)" Type="ColumnType.String" IsPrimaryKey="true"></GridColumn>
<GridColumn Field="@nameof(AuthorizedUser.Role)"></GridColumn>
</GridColumns>
</SfGrid>
@code{
private SfDropDownList<string, string> dropDownReadOk;
private SfDropDownList<string, RoleFields> dropDownWriteOk;
private readonly List<string> dropDownReadValues = new List<string>();
private List<AuthorizedUser> Users { get; set; }
private SfTextBox userId { get; set; }
public class RoleFields
{
public int ID { get; set; }
public string Text { get; set; }
}
private readonly List<RoleFields> roles = new List<RoleFields>()
{
new RoleFields { ID = (int)Role.Admin, Text = Role.Admin.ToString() },
new RoleFields { ID = (int)Role.Basic, Text = Role.Basic.ToString() }
};
protected override void OnInitialized()
{
Users = new List<AuthorizedUser>()
{
new AuthorizedUser { Id = "[email protected]", Role = Role.Admin }
};
foreach (string item in Enum.GetNames(typeof(Role)))
{
dropDownReadValues.Add(item);
}
}
public void OnActionBegin(ActionEventArgs<AuthorizedUser> args)
{
if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save)
{
args.Data.Id = userId.Value;
args.Data.Role = (Role)Enum.Parse(typeof(Role), dropDownReadOk.Value);
// ===> dropDownWriteOk.Value is null !!!!
}
}
public class AuthorizedUser
{
public string Id { get; set; }
public Role Role { get; set; }
public string RoleAsText
{
get { return Role.ToString(); }
set
{
if(value == null)
return;
Role = (Role)Enum.Parse(typeof(Role), value, true);
}
}
}
public enum Role
{
Admin,
Basic
}
}
Hi Han,
Greetings from Syncfusion support.
We have checked your query and prepared an sample as per your requirement. We have prepared an sample solution with dialog editing using enum dropdown. Kindly check the attached sample for your reference.
Kindly get back to us if you still have any difficulties in achieving your requirement or if we misunderstood your query.
Regards,
Monisha
Hi Monisha,
Thank you very much.
Yi
Hi Yi Han,
Welcome. Kindly get back to us if you have further queries. As always we will be happy to assist you.
Regards,
Monisha