I have a grid which has a column bound to an enum. It is displayed correctly but when I want to edit the value it is only possible to enter the integers representing the enum. I found multiple solutions to set a editor with a dropdown for ASP .NET but not for ASP .NET Core for example this: https://stackoverflow.com/a/31402262/3187001
Which seems to be the way to go to handle this but I did not found any solution working with ASP .NET Core.
My view:
@{
ViewData["Title"] = "Test";
}
@(
Html.Kendo().Grid<WebApp.Models.TestModel>
()
.Name("TestGrid").Height(550).Editable(editable => editable.Mode(GridEditMode.InCell))
.ToolBar(toolbar =>
{
toolbar.Save();
})
.Columns(cols =>
{
cols.Bound(col => col.Name);
cols.Bound(col => col.Number);
cols.Bound(col => col.State);
})
.Sortable().Scrollable().DataSource(ds => ds
.Ajax().Batch(true).Model(model =>
{
model.Id(tpn => tpn.Name);
model.Field(tpn => tpn.Name).Editable(false);
model.Field(tpn => tpn.Number);
model.Field(tpn => tpn.State);
}
)
.ServerOperation(false)
.Update(update => update.Action("UpdateTestModels", "Test"))
.Read(read => read.Action("ReadTestModels", "Test")
)
)
)
My controller:
public class TestController : Controller
{
public IActionResult Index()
{
return View("Test");
}
public IActionResult ReadTestModels([DataSourceRequest] DataSourceRequest request)
{
var models = new List<TestModel>();
models.Add(new TestModel() { Name = "Model 1", Number = 1, State = TestModelState.StateA });
models.Add(new TestModel() { Name = "Model 2", Number = 1337, State = TestModelState.StateB });
models.Add(new TestModel() { Name = "Model 3", Number = 42, State = TestModelState.StateC });
return Json(models.ToDataSourceResult(request));
}
[AcceptVerbs("Post")]
public IActionResult UpdateTestModels([DataSourceRequest] DataSourceRequest request,
[Bind(Prefix = "models")] IEnumerable<TestModel> testModels)
{
Console.WriteLine($"{testModels.Count()} Testmodels updated...");
return Json(testModels.ToDataSourceResult(request));
}
}
Model:
public class TestModel
{
public string Name { get; set; }
public int Number { get; set; }
public TestModelState State { get; set; }
}
public enum TestModelState
{
Undefined = 0,
StateA = 1,
StateB = 2,
StateC = 3
}