Question

In my controller, I get a list of brands:

        private IEnumerable<Brand> GetBrands()
    {
        List<Brand> brandList = new List<Brand>();

        List<Brand> brands = Brand.GetList();
        brands = brands.OrderBy(b => b.BrandCode).ToList();

        return brands;
    }

I then stuff that list in a ViewBag for my Edit view:

        [HttpGet]
        public ActionResult Edit(int id)
        {
         this.ViewBag.BrandList = this.GetBrands();
        }

And in my Edit view, I'm trying to implement autocomplete:

    <script type="text/javascript">
    $(document).ready(function () {
        var brandList = @Html.Raw(new JavaScriptSerializer().Serialize(this.ViewBag.BrandList));
        $('#BrandCode').autocomplete({
            source: brandList,
            minLength: 3
        });

        console.log(brandList);
    });
</script>

From the console logging, I see the brandList is a list of Objects:

console log

But I can't get a match in the BrandCode field? What am I doing wrong?

autocomplete

EDIT: Here is the Brand entity:

public class Brand : PersistantEntity
{
    private IBrandRepository repository;

    public int Id
    {
        get { return this.repository.Id; }
        set { this.repository.Id = value; }
    }

    public string BrandCode
    {
        get { return this.repository.BrandCode; }
        set { this.repository.BrandCode = value; }
    }

    protected override Repository.Abstract.IRepository Repository
    {
        get { return this.repository; }
        set { this.repository = value as IBrandRepository; }
    }

    public Brand()
    {
        this.repository = RepositoryFactory.CreateFromConfig<IBrandRepository>();
    }

    internal Brand(IBrandRepository repository)
    {
        this.repository = repository;
    }

}
Was it helpful?

Solution

I changed the definition of GetBrands to return a list of strings (BrandCodes) instead of Brand objects. This fixed the problem. Here's the new definition:

        private IEnumerable<string> GetBrands()
    {
        List<string> brandList = new List<string>();

        List<Brand> brands = Brand.GetList();
        brands = brands.OrderBy(b => b.BrandCode).ToList();

        foreach (var brand in brands)
        {
            brandList.Add(brand.BrandCode.ToString()); 
        }

        return brandList;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top