Hi,
i searched probably every thread in this forum but was not able to find the solution for this.
I am using an .NET 6 application and i am trying to display enum values in a grid and need to have all options as a dropdown in edit mode.
My Class:
public class Kunde
{
public int Id { get; set; }
public string? Name { get; set; }
public string? FullName { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public KundenArt KundenArt { get; set; }
}
public enum KundenArt
{
[EnumMember(Value = "Zielkunde")]
Zielkunde = 0,
[EnumMember(Value = "Bestandskunde")]
Bestandskunde = 1,
[EnumMember(Value = "Verlustkunde")]
Verlustkunde = 2
}
My Grid:
<ejs-grid locale="de-DE" id="Grid" height="600" width="auto" allowPdfExport="true" allowExcelExport="true" pdfExportComplete="pdfExportComplete" excelExportComplete="excelExportComplete" toolbar="@(new List<string>() { "Edit", "Refresh", "ExcelExport" })" toolbarClick="toolbarClick" enableStickyHeader="true" allowFiltering="true" allowGrouping="true" allowSorting="true" actionFailure="actionFailure">
<e-data-manager url="/Kunden/GetGridData" updateUrl="/Kunden/Update" removeUrl="/Kunden/Delete" adaptor="UrlAdaptor"></e-data-manager>
<e-grid-editSettings allowDeleting="true" allowEditing="true" newRowPosition="Top" mode="Dialog"></e-grid-editSettings>
<e-grid-filtersettings type="CheckBox"></e-grid-filtersettings>
<e-grid-columns>
<e-grid-column field="Id" headerText="Id" isPrimaryKey="true" visible="false" textAlign="Right" width="140" ></e-grid-column>
<e-grid-column field="Name" headerText="Name" allowediting="false" width="150"></e-grid-column>
<e-grid-column field="FullName" headerText="FullName" width="250"></e-grid-column>
<e-grid-column field="KundenArt" headerText="Kundenart" editType="dropdownedit" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
My Data URL Function:
public ActionResult GetGridData([FromBody] DataManagerRequest dm)
{
IEnumerable DataSource = _context.Kunde.OrderBy(x => x.Id).ToList();
DataOperations operation = new DataOperations();
List<string> str = new List<string>();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Kunde>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
}
Can someone help me here?
Hi Mark,
Thanks for contacting Syncfusion support.
Query: i am trying to display enum values in a grid and need to have all options as a dropdown in edit mode.
By analyzing your query, we suspect that you want to show all the EnumString values as options in the dropdown while editing that column in the Grid. If so, you can achieve this by using cellEditTemplate feature of Grid. In which you can bind the whole options as dataSource to the dropdown component.
cellEditTemplate: https://ej2.syncfusion.com/aspnetcore/documentation/grid/edit/#cell-edit-template
By using cellEditTemplate feature, you can render custom edit control in the column. The cell edit template is used to add a custom component for a particular column by invoking the following functions:
|
[index.cshtml]
<e-grid-column field="State" headerText="State" editType="dropdownedit" edit="@(new {create = "create", read = "read", destroy = "destroy", write = "write"})" width="150"></e-grid-column>
<script> var ddlObject; var data = new ej.data.DataManager({ url: '/Home/UrlDropDatasource', adaptor: new ej.data.UrlAdaptor(), crossDomain: true }); // get All EnumString values from the server
function create() { // create a input element return document.createElement('input'); }
function read(args) { // return the value which will be saved in the grid return ddlObject.value; }
function destroy() { // destroy the custom component. ddlObject.destroy(); }
function write(args) { // create a dropdown control ddlObject = new ej.dropdowns.DropDownList({ // bind the data as per your requirement dataSource: data, //bind the current cell value to the Dropdown which will be displayed only when the dropdown dataSource contains that value value: args.rowData[args.column.field], //map the appropriate columns to fields property fields: { text: 'State', value: 'State' }, //set the placeholder to DropDownList input placeholder: "Find a customer" }); //render the component ddlObject.appendTo(args.element); }
</script>
|
Please get back to us if you need further assistance with this.
Regards,
Rajapandiyan S
Hey,
thanks for your help.
I will try that for the edit mode.
FIrst of all, my values are still displayed as numbers in the grid. (0,1,2) - How can i solve this issue?
2nd: You are using .net 3 core in your solution. I am using .NET 6. Any solutions for that?
Best regards,
Mark
So i got 2 problems:
First: If i check my Datasource for the grid , i see the "String"-Values in my Objects - but in the grid the number is still displayed.
Second:
While implementing the custom Dropdown for edit-mode, i get the following JS-Error:
"Uncaught ReferenceError: ej is not defined"
I use this bundle:
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js"></script>
Hi Mark,
Currently, we are preparing the Grid sample with .NET 6 and we will update the further details on or before Apr 27th, 2022.
We appreciate your patience until then.
Regards,
Rajapandiyan S
Awesome -
looking forward to hear from you about it!
Hi Mark,
Thanks for your patience.
Query #1: You are using .net 3 core in your solution. I am using .NET 6. Any solutions for that?
We have prepared a sample with .NET 6, you can get it from the below link.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/core_net6_grid_enum1193571622.zip
Query #2: FIrst of all, my values are still displayed as numbers in the grid. (0,1,2) - How can i solve this issue?
By default .Net core application returns enum values(int) instead of names. We can return the Enum string by using the following way,
|
public class BigData { public int? OrderID { get; set; } --- [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] // returns the enum text public state State { get; } }
public enum state { [EnumMember(Value = "Delivery")] Delivery = 0, [EnumMember(Value = "Warehouse")] Warehouse = 1, [EnumMember(Value = "Drying")] Drying = 2, [EnumMember(Value = "Dried")] Dried = 3, [EnumMember(Value = "Cutting")] Cutting = 4, [EnumMember(Value = "Cutted")] Cutted = 5 }
|
|
[program.cs]
using Newtonsoft.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container. builder.Services.AddControllersWithViews();
builder.Services.AddMvc().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });
|
Query #3: While implementing the custom Dropdown for edit-mode, i get the following JS-Error: "Uncaught ReferenceError: ej is not defined"
We are unable to replicate this issue at our end. Kindly share the below details to replicate this.
|
function actionFailure(args) { console.log(args); }
|
Regards,
Rajapandiyan S
For everyone with the same problem:
The missing piece was:
builder.Services.AddMvc().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
with the following nuget package:
The Value is now displayed with the correct names !
Regarding the "ej" error:
<script>
var ddlObject;
var data = new ej.data.DataManager({
url: '/Home/UrlDropDatasource',
adaptor: new ej.data.UrlAdaptor(),
crossDomain: true
});
this was executed before the grid was loaded. Therefore, ej was not defined yet. After putting the function into the "created" event of the grid, everything is working.
Thanks for your help!
Hi Mark,
We are glad to hear that you have resolved the reported problem with the provided sample.
Please get back to us if you need further assistance.
Regards,
Rajapandiyan S