Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using Serge Zab's helper for optgroup dropdowns

I'm trying to work with the optgroup dropdown helper from Serge Zab that can be found here.

This is my category table:
enter image description here

As you can see I have a category and a category can also be categoryparent from a category. I want to have the parentcategorys as optgroup and the children as options of the optgroup. (Only the children can be selected)

In my ViewModel:

public short? CategoryId { get; set; }
public IEnumerable<ReUzze.Helpers.GroupedSelectListItem> GroupedTypeOptions { get; set; }

In my Controller:

[Authorize] // USER NEEDS TO BE AUTHORIZED
public ActionResult Create()
{
    ViewBag.DropDownList = ReUzze.Helpers.EnumHelper.SelectListFor<Condition>();

    var model = new ReUzze.Models.EntityViewModel();
    PutTypeDropDownInto(model);
    return View(model);
}

[NonAction]
private void PutTypeDropDownInto(ReUzze.Models.EntityViewModel model)
{
    model.GroupedTypeOptions = this.UnitOfWork.CategoryRepository.Get()
         .OrderBy(t => t.ParentCategory.Name).ThenBy(t => t.Name)
         .Select(t => new GroupedSelectListItem
         {
             GroupKey = t.ParentId.ToString(),
             GroupName = t.ParentCategory.Name,
             Text = t.Name,
             Value = t.Id.ToString()
         }
     );
}

In my View:

@Html.DropDownGroupListFor(m => m.CategoryId, Model.GroupedTypeOptions,  "[Select a type]")    

When I try to run this I always get the error:

Object reference not set to an instance of an object.

I get this error on this rule : .OrderBy(t => t.ParentCategory.Name).ThenBy(t => t.Name)

Can anybody help me find a solution for this problem?

like image 552
nielsv Avatar asked Dec 30 '13 23:12

nielsv


2 Answers

Your error message suggests that either a t is null or a t.ParentCategory is null.

You can fix the error by simply checking for nulls, but this may or may not give you the desired output, depending on whether you also want to include categories that don't have a parent.

model.GroupedTypeOptions = this.UnitOfWork.CategoryRepository.Get()
     .Where(t => t.ParentCategory != null)
     .OrderBy(t => t.ParentCategory.Name).ThenBy(t => t.Name)
     .Select(t => new GroupedSelectListItem
     {
         GroupKey = t.ParentId.ToString(),
         GroupName = t.ParentCategory.Name,
         Text = t.Name,
         Value = t.Id.ToString()
     });

I'm assuming your CategoryRepository can't return a null t, but if it can, you'd adapt the where to be:

.Where(t => t != null && t.ParentCategory != null)
like image 89
John H Avatar answered Nov 15 '22 04:11

John H


The problem is that not all of your entities returned will have a ParentCategory.

It seems you should only be selecting the children and not the parents.

Try this:

model.GroupedTypeOptions = this.UnitOfWork.CategoryRepository.Get()
     .Where(t => t.ParentCategory != null)
     .OrderBy(t => t.ParentCategory.Name).ThenBy(t => t.Name)
     .Select(t => new GroupedSelectListItem
     {
         GroupKey = t.ParentId.ToString(),
         GroupName = t.ParentCategory.Name,
         Text = t.Name,
         Value = t.Id.ToString()
     }
 );
like image 41
Christopher.Cubells Avatar answered Nov 15 '22 03:11

Christopher.Cubells