AjaxCheckBoxList Error

Dec 5, 2011 at 2:15 PM

Hello friends,
I am the following problem:
When you load the checkboxlist  
parentId relative error appears:

j is null
var sel = j.Selected ? 'checked="checked"' : ""; 
Awesome.js (linha 89)


The checkboxlist child is with the text "undefined"
When you select a parent item, back to normal, but if you clear all the error appears.

What can I
be doing wrong?

My view code:
      @using (Html.BeginForm())
      {
         
         @Html.AjaxCheckBoxList("Products") 
         @Html.AjaxCheckBoxList("Category", parentId: "Products")
         
      }
My controller code:
 public class ProductsAjaxCheckBoxListController : Controller
    {
        private IProductRepository repository;

        public ProductsAjaxCheckBoxListController(IProductRepository repo)
        {
            repository = repo;
        }

        public ActionResult GetItems(IEnumerable<string> keys)
        {
            keys = keys ?? new List<string>();
            var res =
                repository.Products.Select(
                    o =>
                    new SelectListItem
                        {
                            Text = o.Category,
                            Value = o.Category,
                            Selected = (keys.Contains(o.Category))
                        }).Distinct();
            return Json(res);
        }
    }

    public class CategoryAjaxCheckBoxListController : Controller
    {

        private IProductRepository repository;

        public CategoryAjaxCheckBoxListController(IProductRepository repo)
        {
            repository = repo;
        }

        public ActionResult GetItems(IEnumerable<int> keys, IEnumerable<string> parent)
        {
            if (parent == null) return new EmptyResult();
            keys = keys ?? new List<int>();
            var res =
                repository.Products.Where(o => o.Category != null && parent.Contains(o.Category)).Select(
                    o =>
                    new SelectListItem
                        {
                            Text = o.Name,
                            Value = SqlFunctions.StringConvert((double) o.ProductID),
                            Selected = (keys.Contains(o.ProductID))
                        });
            return Json(res);
        }
    }


 

 

Dec 5, 2011 at 3:11 PM

try debugging your actions, probably you get some errors there

Dec 5, 2011 at 3:31 PM

No errors found in the actions.

may be because I'm using IQueryable <Product> Products {get;} ?

IQueryable not IEnumerable?

Dec 5, 2011 at 4:45 PM

I found the error:
The problem is in version 1.7.1 of jQuery
http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js

this version does not appear any error
http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js
But version 1.7.1 show error:
j is null
var sel = j.Selected ? 'checked="checked"' : ""; 
Awesome.js (linha 89)

any suggestions?
Dec 6, 2011 at 5:48 AM

you can call at the end .AsEnumerable() and it will be IEnumerable,

write some unit tests and call these actions in there see what they return

Dec 6, 2011 at 12:49 PM
Edited Dec 6, 2011 at 12:52 PM

But AjaxCheckBoxList does not work properly with jquery version http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js

show error:

j is null
var sel = j.Selected ? 'checked="checked"' : ""; 
Awesome.js (linha 89)

with version 1.4.4 work properly.
Replace the version and now is ok.
Dec 14, 2011 at 12:01 PM
Edited Dec 14, 2011 at 12:03 PM

http://stackoverflow.com/questions/6831011/checked-checked-not-rendering-as-checked-radio-button-in-chrome

Dec 14, 2011 at 4:18 PM

thanks. worked with 1.6.2 jquery version

http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

Dec 15, 2011 at 1:55 PM

sorry, did some more tests and the problem continues in the checklist. It only works in version 1.4.4.
I think it's a bug in awesome.js

Dec 15, 2011 at 3:04 PM
Edited Dec 15, 2011 at 3:05 PM

could be, I use a new approach in the new version, so it is going to work in all versions of jquery