0

Client code

var availableTags = ["tag1", "tag2"];

I take tags from database, how to create variable in mvc 5? I want to write this:

var availableTags = @ViewBag.Tags;

I've tried

ViewBag.Tags = JsonConvert.SerializeObject( new List<string>() { "Tag1", "Tag2" });

But this doesn't work

3
  • how you want you use availableTags? Commented Aug 22, 2016 at 7:36
  • var availableTags = @Html.Raw(Json.Encode(ViewBag.Tags))'; Commented Aug 22, 2016 at 7:36
  • @AdilMammadov As source for tagit.js, ["tag1","tag2"] - I need this format Commented Aug 22, 2016 at 7:37

1 Answer 1

2

You can use Json.Encode.

Server side

ViewBag.Tags = new List<string>() { "Tag1", "Tag2" };

Client Side

var availableTags = @Html.Raw(Json.Encode(ViewBag.Tags)); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.