0

I have an application I want to add tags input box

I used http://aehlke.github.io/tag-it/examples.html library

Controller Create Method:

        // GET: Posts/Create
    public ActionResult Create()
    {
        var Tags = db.MyTags.Select(t=>t.TagName).ToArray();

        ViewBag.ExistingTags = Tags;
        return View();
    }

View:

I have below script

<script>
    $(function(){
        //var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'go', 'lua'];
        var sampleTags = JSON.stringify(@ViewBag.ExistingTags);

</script>

how can I get the passed ExistingTags array as the sample code commented

MJ X
  • 8,506
  • 12
  • 74
  • 99

1 Answers1

1

Use @Html.Raw() and Json.Encode() as shown :-

<script>
    $(function(){
        //var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'go', 'lua'];
        var sampleTags = @Html.Raw(Json.Encode(@ViewBag.ExistingTags));
</script>
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69