Search code examples
jqueryasp.net-mvc-3tag-it

Tag-it onlyAvalaibleTags option doesn't work


I use tag-it plugin from https://github.com/aehlke/tag-it/downloads. How to disable adding new tags?

$(document).ready(function () {
                $("#Tags").tagit({
                    singleField: true,
                    singleFieldNode: $('#mySingleField'),
                     //  onlyAvailableTags : true,
                     allowNewTags: false,
                    tagSource: [@Html.Raw(ViewBag.AvailableTags)]
                });
            });

I tried to use onlyAvailableTags : true and allowNewTags: false options, but there's no effect.


Solution

  • Since you say "but there's no effect", I would guess that @Html.Raw(ViewBag.AvailableTags) produces an output that that breaks the javascript syntax. The tags need to be in quotes, otherwise they are treated as variables.

    Incorrrect output:

    tagSource: [my-tag, another-tag]
    

    Server-side, I assume you have some kind of IEnumerable<string>:

    ViewBag.AvailableTags = new List<string>
    {
        "my-tag",
        "another-tag",
    };
    

    Then, in your .cshtml:

    tagSource: ["@string.Join("\", \"", ViewBag.AvailableTags)"]
    

    This would produce the correct output:

    tagSource: ["my-tag", "another-tag"]
    

    That would be what I'd try first.