0

I am trying to do what seems like the simplest thing in the world, but it has me absolutely banging my head against the wall in frustration. I just need to pass a simple string array from a C# method in my Controller class to javascript code in my index view. That's it. Just pass a simple array. But damned if I can figure out how, or make any sense of any of the convoluted answers I've seen on the internet.

Here are the details (simplified):

My controller:

public class BobsController : Controller
{
    // GET: Bobs
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult ReturnBobsStuff()
    {
    List<string> myList = new List<string> { "element1", "element2", 
                                             "element3", "element4", 
                                             "element5", 
                                           };
    string[] myArray = myList.ToArray();
    return View(myArray);
    }
 }

My view:

<script>
    $(function () {
            var JavascriptArray = ??;
          });
</script>

The ?? is the problem, obviously.

7
  • You need to learn how to make ajax calls using jquery or some other framework. Its asynchronous in nature so you will need to learn how to work with promises as well (or observables if you decide to go with rxjs). Generally you use web-api to expose the server side end points (not mvc). Commented Oct 29, 2018 at 21:24
  • I know how to make an ajax call, but how do I get the array out of the Ajax call and into a variable I can use in my javascript? This can't be that complicated. It's a simple array. I thought the whole point of MVC was to make it easy to mix c# code in controllers with javascript code in views. And you can't pass a simple string array between the two?? Commented Oct 29, 2018 at 21:40
  • put the variable in the viewbag...then in the view where your ?? is, serialise it as JSON so that it will be injected into the JS as a literal. You don't need to make it part of your model unless it's going to be used in some Razor or something Commented Oct 29, 2018 at 21:56
  • 1
    The question duplicates this topic. Commented Oct 29, 2018 at 22:02
  • 1
    Possible duplicate of Razor MVC Populating Javascript array with Model Array Commented Oct 29, 2018 at 22:31

1 Answer 1

1

Try this in your controller:

 public ActionResult ReturnBobsStuff()
{
List<string> myList = new List<string> { "element1", "element2", 
                                         "element3", "element4", 
                                         "element5", 
                                       };
ViewBag.MyVariable = myList.ToArray();
return View();
}

Then in your view:

 <script> var myJsVariable = 'ViewBag.MyVariable' </script>

But I suggest for you to try other efficient method, like using an AJAX call.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.