AjaxDropdown and the checkboxlist

Jul 22, 2011 at 2:56 PM

I have a view model as shown below. My problem is that the Category is being required on the UI even without the Required attribute. If I use and AjaxCheckBoxList instead of an AjaxDropDown then Category is not being required.  But checkboxlist doesn't serve my purpose. I need a dropdown for category to filter out Products (checkboxes).

 public class FormViewModel
    {
        [UIHint("AjaxDropdown")]
        [AjaxDropdown(Controller="CategoryAjaxDropdown")]
        public Guid Category { get; set; }

        [Required(ErrorMessage="Please select at least one product")]
        [UIHint("AjaxCheckBoxList")]
        [AjaxCheckBoxList(ParentId = "Category")]
        public IEnumerable<Guid> Products { get; set; }
    } 

Jul 22, 2011 at 3:04 PM

put a Guid? and it's not going to be required

all types that have value by default are required by default

Jul 22, 2011 at 3:13 PM

It worked! Thanks!

So the Required attribute is being overridden. That one is hard to catch.

Also, I noticed for a checkboxlist with a ParentId, it will not load the first time if the ParentId is declared as NOT nullable. If the ParentId is nullable then the checkboxlist loads right away - page load.

Jul 22, 2011 at 4:41 PM

I have a related problem. Although the GetItems (checkboxlist) is being triggered the parent parameter is null. Using a similar sample above:

public class FormViewModel
    { 
        public Guid Category { get; set; }

        [Required(ErrorMessage="Please select at least one product")]
        [UIHint("AjaxRadioList")]
        [AjaxRadioList(ParentId = "Category")]
        public IEnumerable<Guid> Products { get; set; }
    }

This is my controller:

  public class ProductsAjaxRadioListController : Controller
    {
        public ActionResult GetItems(Guid? key, Guid? parent)
        {
        // parent is null on first call
        }
    }

FormViewModel.Category is not null and not Guid.Empty. But when GetItems is called, the "parent" parameter is NULL. How can GetItems be called with default non-null parent.

Jul 22, 2011 at 5:28 PM

no it's not overridden, that's how C# works, valuetypes must have value, if for an int or guid comes a null value the ModelBinder can't  set the value for that property so it puts an error, so the ModelsState is not valid anymore

Jul 22, 2011 at 5:33 PM

about the problem not sure, if it still doesn't work make a sample project upload it somewhere and give me the link

Jul 25, 2011 at 1:09 PM

Right it was the ModelBinder. Thanks o!