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

Null value for empty entries in DropDown

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?

3 Replies

BM Balaji Marimuthu Syncfusion Team March 9, 2016 02:16 PM UTC

Hi Omar,

Thanks for contacting Syncfusion support.

You have processed the correct way and it’s the best approach. So we suggest you to the same, and get back to us if you need any further assistance.

Regards,
Balaji Marimuthu


OM Omar Muscatello March 9, 2016 05:22 PM UTC

Ok, thank you Balaji Marimuthu.


BM Balaji Marimuthu Syncfusion Team March 10, 2016 04:05 AM UTC

Hi Omar,

Thanks for the update.

Please get back to us if you need any further assistance.

Regards,
Balaji Marimuthu


Loader.
Up arrow icon