Kendoui Autocomlete viewbag is not working

2 posts, 0 answers
  1. Elif
    Elif avatar
    1 posts
    Member since:
    May 2012

    Posted 10 Nov 2012 Link to this post

    I can not set viewbag.x to datasource property of kendoAutoComplete.

    var friends = "@ViewBag.Friends2";
    var friends2 = $('<div/>').html(friends).html();

    $.each(friends2, function (key, value)
    $("#pResult").append(key.Name +" " + value.Name);

    when I try change the datasource viewbag to other ,it worked.
  2. Burke
    Burke avatar
    41 posts
    Member since:
    Jun 2009

    Posted 30 Nov 2012 Link to this post

    Hi Elif!

    It depends really on what the format of your data in the ViewBag is.  You need to convert it to a JavaScript array for the grid.  Razor is really good at this.  If you are returning a list of friends from your controller like this...
    public ActionResult Index()
       ViewBag.Friends = new List<string>() {
      return View();
    You then need to encode it as JSON so that it's readable in JavaScript.  Othewise Razor will just insert the type where @ViewBag.Friends is.
      dataSource: @Html.Raw(Json.Encode(@ViewBag.Friends))
Back to Top