本文介绍了转换viewbag到JavaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从ViewBag.mytags到一个Javascript数组中的数据,但我没能avhice这种

  $(函数(){
    VAR sampleTags =新的Array();
    VAR阵列= @ Html.Raw(Json.En code(@ ViewBag.mytags));
    对于(VAR I = 0; I< array.length,我++){
        sampleTags [I] =阵列[我]
    }
    $('#singleFieldTags')。tagit({
        availableTags:sampleTags,
        singleField:真实,
        singleFieldNode:$('#mySingleField')
    });
}

这是我的控制器

  ViewBag.mytags = mp3.TagSuggestion();

这是我的模型

 公开的IQueryable<串GT; TagSuggestion()
{    IQueryable的<串GT;在db.tblTags标签=从s选择s.Title;    返回标签;}


解决方案

 请按照以下步骤操作
    公众的IList<串GT; TagSuggestion()
{    IQueryable的<串GT;在db.tblTags标签=从s选择s.Title;    返回tabs.toList();}
    1. ViewBag.mytags = mp3.TagSuggestion()了ToList()。
    2。
    $(函数(){
    VAR sampleTags =新的Array();
    VAR阵列= @ Html.Raw(Json.En code(@ ViewBag.mytags));
     对于(VAR I = 0; I< array.length,我++){
        sampleTags [I] =阵列[我]
    }
    $('#singleFieldTags')。tagit({
        availableTags:sampleTags,
        singleField:真实,
        singleFieldNode:$('#mySingleField')
    });
    }

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;

}
解决方案
    Please follow these step
    public IList<string> TagSuggestion()
{

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

    return tabs.toList();

}
    1. ViewBag.mytags = mp3.TagSuggestion().toList();
    2.
    $(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到JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:08