Hello Syncfusion Team,
I'm working with the Grid control.
The grid show a nested entity throught:
col.Field(x => x.Categoria.Id).HeaderText("Categoria")
.ForeignKeyField("Id").ForeignKeyValue("Nome")
.DataSource(ViewBag.Categorie)
.EditType(EditingType.Dropdown)
.Add();
I want to allow the user to set an empty value for this field. In the controller I added:
public ActionResult Index()
{
var categorie = new DataDbContext().Categorie.ToList();
categorie.Add(new Categoria()); // Empty entry, Id = 0
ViewBag.Categorie = categorie;
return View();
}
In this way, it shows the empty entry.
In the controller, to save the changes I use:
[HttpPost]
public JsonResult Modifica(PazienteViewModel.Index value)
{
using (DataDbContext db = new DataDbContext(false))
{
Paziente mPaziente = db.Pazienti.Find(value.Id);
db.Entry(mPaziente).CurrentValues.SetValues(Mapper.Map<Paziente>(value));
mPaziente.IdCategoria = value.Categoria?.Id == 0 ? null : value.Categoria?.Id; // When the user selects the empty entry, value.Categoria.Id = 0, so I have to check if 0 and replace with null.
db.SaveChanges();
return Json(true);
}
}
Is there any alternative for the code highlighted in yellow?