我希望将ViewBag.mytags中的数据转换为Javascript数组,但我无法将其视为
$(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')
});
}
这是我的控制器
ViewBag.mytags = mp3.TagSuggestion();
这是我的模特
public IQueryable<string> TagSuggestion()
{
IQueryable<string> tabs = from s in db.tblTags select s.Title;
return tabs;
}
答案 0 :(得分:9)
请按照以下步骤进行操作
public IList<string> TagSuggestion()
{
IQueryable<string> tabs = from s in db.tblTags select s.Title;
return tabs.toList();
}
内部MVC控制器:
ViewBag.mytags = mp3.TagSuggestion().toList();
在视图中:
<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>