This is a migrated thread and some comments may be shown as answers.

Kendoui Autocomlete viewbag is not working

1 Answer 119 Views
AutoComplete
This is a migrated thread and some comments may be shown as answers.
Elif
Top achievements
Rank 1
Elif asked on 10 Nov 2012, 11:33 PM



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.

1 Answer, 1 is accepted

Sort by
0
Burke
Top achievements
Rank 1
answered on 30 Nov 2012, 05:25 PM
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>() {
     "Burke",
     "Brandon",
     "Todd",
     "Derick",
     "Elif"
  };
  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.
$("#auto").kendoAutoComplete({
  dataSource: @Html.Raw(Json.Encode(@ViewBag.Friends))
});
Cheers!
Tags
AutoComplete
Asked by
Elif
Top achievements
Rank 1
Answers by
Burke
Top achievements
Rank 1
Share this question
or