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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Null value for empty entries in DropDown

Thread ID:

Created:

Updated:

Platform:

Replies:

123326 Mar 8,2016 03:11 PM UTC Mar 10,2016 04:05 AM UTC ASP.NET MVC 3
loading
Tags: Grid
Omar Muscatello
Asked On March 8, 2016 03:11 PM UTC

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?

Balaji Marimuthu [Syncfusion]
Replied On 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

Omar Muscatello
Replied On March 9, 2016 05:22 PM UTC

Ok, thank you Balaji Marimuthu.

Balaji Marimuthu [Syncfusion]
Replied On 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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;