2

In this controller i got all the movienames

public ActionResult MultiText()
 {
    List<string> mnames = db.AllMovienames().ToList();
    ViewBag.movies = mnames;

    return View();
}

I need to send this "ViewBag.movies" to Javascript present in view

 $(function () {

    var availableTags = ViewBag.movies;
    function split(val) {
        return val.split(/,\s*/);
    }
    function extractLast(term) {
        return split(term).pop();
    }
    $("#tags")
    // don't navigate away from the field on tab when selecting an item
  .bind("keydown", function (event) {
   if (event.keyCode === $.ui.keyCode.TAB &&
   $(this).data("ui-autocomplete").menu.active) {
    event.preventDefault();
  }
})

But i cant get values into view

3 Answers 3

2

You could write it out as json in your view like this:

<script type="text/javascript">
 var movies =  @Html.Raw(Json.Encode(ViewBag.movies))
</script>

If you wrap your javascript up into a function you could then pass them in i.e.

function DoThis(movies)
{
   var availableTags = movies;
    .....
}

This would allow you to call it like this:

<script type="text/javascript">
 var movies =  @Html.Raw(Json.Encode(ViewBag.movies))
 DoThis(movies);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

it show compile time error.is there any assembly i need to give?
System.Web.Helpers.Json is the assembly
0

You could put the tags into a hidden field and then retrieve them using jQuery:

View:

 @Html.Hidden("Movies", string.Join(",", ViewBag.movies))

JavaScript:

 var availableTags = $('#Movies').val().split(",");

Comments

0

you can use the code below:

/In Controller

/Controller/SiteConfig
public ActionResult SiteConfig()
{
     string movie = // your code
     return JavaScript("var movies ="+moive);
}
//In cshtml
@Script.Render("~/Controller/SiteConfig")

Then you can get variable movie in javascript.

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.