0

I've created a JSON array from a database and I have it set up in a controller like so:

public ActionResult highlight()
{
    var statesHighlight =
        db.Jobs
            .Select(r => r.State);
    return Json(statesHighlight , JsonRequestBehavior.AllowGet);
}

How do I set this as a JavaScript variable in my view? I'm sure there's a simple way, but I can't seem to figure it out. Do I use @Html.Action or @Url.Action?

EDIT: Here's how I currently have my data hard coded for the plugin I'm using:

<script type="application/json" id="map-data">
    ["CA","UT","FL","MT","WA","KS","KS","UT"]
</script>

<script>
    var data = $.parseJSON($('#map-data').text());
    var cities = $.parseJSON($('#city-data').text());


    $('img').mapster({
        mapKey: 'state',
        clickNavigate: true,
        isSelectable: false,
        highlight: false,
        onConfigured: function () {

            // make the array into a comma-sparated list
            var csv = data.join(',');

            var city_list = cities.join(',');

            // the 'set' activates the areas
            $('img').mapster('set', true, csv, options = { fillColor: '638EA5' });
            //altImage: '../Images/map_outline_blackStrokeDot.png'
            $('img').mapster('set', true, city_list, options = { fillColor: 'ffffff' });
        }
    });
</script>
3
  • Is this being returned to an ajax call? Commented May 6, 2015 at 21:46
  • No, I'm using the data for a plugin called ImageMapster to highlight areas on an image map. Commented May 6, 2015 at 21:48
  • I don't know anything about that plugin but you need to make that clear in the question and show the relevant scripts you are using Commented May 6, 2015 at 22:00

1 Answer 1

1

Check this out:

<script type="text/javascript">
    $(function () {
        $.getJSON('gimmearray'
            , null
            , function (data) {
                console.log(data);
            });
    });
</script>

And the controller:

    public JsonResult GimmeArray()
    {
        string[] theArray = {"this", "is", "an", "array"};

        return Json(theArray, JsonRequestBehavior.AllowGet);
    }

On you controller, you probably have to execute that query using ToArray.

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.