3

こんにちは、コントローラーに次のコードがあります。

   ViewBag.localitate = db.Localitatis.OrderBy(p => p.Localitate).ToList();

ビューで、次の方法で取得しようとします:

<script type="text/javascript">
    $(function () {
        var availableTags = [
         @foreach (var item in ViewBag.localitate)
              {
                       @item.Localitate 
             }


    ];
        $("#destination").autocomplete({
            source: availableTags
        });
    });
</script>

jqueryデモの例のように配列を取得するためにこれをフォーマットする方法がわかりません:

"ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"

データベースからの値でオートコンプリートを機能させるために、

ありがとうございました

4

2 に答える 2

2
<script type="text/javascript">
    $(function () {
        $("#destination").autocomplete({
            source: @Html.Raw(Json.Encode(ViewBag.localitate))
        });
    });
</script>

If you have lots of data, consider using a server-side controller action to perform the filtering.

于 2012-08-30T22:31:46.360 に答える
0

First, set your view data to only have the actual names you want:

ViewBag.localitate = db.Localitatis.Select(p => p.Localitate).OrderBy(p => p).ToList();

Then the simplest way to render this in javascript would be:

var availableTags = [" @String.Split("\,\"", ViewBag.localitate) "];

But I'd recommend using a JSON parser like JSON.NET instead. This way you don't have to worry about cross-site scripting attacks and such, since things are escaped for you.

var availableTags = @(JsonConvert.Serialize(ViewBag.localitate));
于 2012-08-30T22:29:53.907 に答える