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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jim Sloun
  • 11
  • 1
  • 4
  • 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). – Igor Oct 29 '18 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?? – Jim Sloun Oct 29 '18 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 – ADyson Oct 29 '18 at 21:56
  • 1
    The question duplicates [this topic](https://stackoverflow.com/questions/23781034/razor-mvc-populating-javascript-array-with-model-array). – Vladimir Ilin Oct 29 '18 at 22:02
  • 1
    Possible duplicate of [Razor MVC Populating Javascript array with Model Array](https://stackoverflow.com/questions/23781034/razor-mvc-populating-javascript-array-with-model-array) – Tetsuya Yamamoto Oct 29 '18 at 22:31
  • In which view you want the array ? The Index view or `ReturnBobsStuff` ? – Shyju Oct 29 '18 at 23:04
  • `var JavascriptArray = @Html.Raw(Json.Encode(Model))` (note also the `myList.ToArray()` is unnecessary) –  Oct 30 '18 at 00:56

1 Answers1

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.

Eleazpogi
  • 31
  • 3