Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  38] [ 1]  / answers: 1 / hits: 19230  / 12 Years ago, mon, september 17, 2012, 12:00:00

I want to get the data from the ViewBag.mytags to a Javascript array, but I was not able to avhice this



$(function () {
var sampleTags = new Array();
var array = @Html.Raw(Json.Encode(@ViewBag.mytags));
for(var i =0; i<array.length;i++){
sampleTags[i] = array[i];
}
$('#singleFieldTags').tagit({
availableTags: sampleTags,
singleField: true,
singleFieldNode: $('#mySingleField')
});
}


This is my controller



ViewBag.mytags = mp3.TagSuggestion();


This is my Models



public IQueryable<string> TagSuggestion() 
{

IQueryable<string> tabs = from s in db.tblTags select s.Title;

return tabs;

}

More From » asp.net-mvc

 Answers
9

Please follow these step



public IList<string> TagSuggestion() 
{
IQueryable<string> tabs = from s in db.tblTags select s.Title;
return tabs.toList();
}


Inside MVC Contoller :



ViewBag.mytags = mp3.TagSuggestion().toList();


In view:



<script>
$(function () {
var sampleTags = new Array();
var array = @Html.Raw(Json.Encode(@ViewBag.mytags));
for(var i =0; i<array.length;i++){
sampleTags[i] = array[i];
}

$('#singleFieldTags').tagit({
availableTags: sampleTags,
singleField: true,
singleFieldNode: $('#mySingleField')
});
});
</script>

[#83056] Friday, September 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristineterrak

Total Points: 74
Total Questions: 109
Total Answers: 115

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;