views:

63

answers:

4

Hi,

I can pass a variable from MVC ASP.NET by using this :

var lastCategoryId = '<%=Model.CS.LastSelectedCategory %>';

This work fine with string or integer but how do I do with an array of strings? I have tried to pass the array the same way but the variable is set to System.String[] ?

BestRegards

A: 

You need to format the array into a JavaScript array syntax.

var someArray = [<%= Model.SomeArray.Select(x => "'" + x +"'")
                           .Aggregate((x,y) => x + ", " + y);  %>];

This will surround each entry by single quotes and then join them together with commas between square brackets.

Updated: removed extra parenthesis.

Matthew Manela
Sorry but I get the following exception with this solution = ) expected
SnowJim
I had an extra parenthesis. I removed it. try now.
Matthew Manela
+2  A: 

something like this:

<script type="text/javascript">
var myArr = [<%=string.Join(",", strArr.Select(o => "\"" + o + "\"")) %>];
</script>
Omu
+1 for being the cleanest, most to-the-point
STW
The problem with this one is that it doesn't wrap the string values in quotes which would cause a JavaScript error.
Matthew Manela
@Matthew Manela thanx for telling me that, fixed it
Omu
@Omu, what if `o` contains a quote? What if it contains a double quote? What if it contains javascript and XSS code that could steal your authentication cookie? Can't even imagine the disasters that can be caused by writing such code.
Darin Dimitrov
@Darin Dimitrov well, your right, that was just a simple way, probably he doesn't need encoding, who knows
Omu
+2  A: 

This should do

var someArray=[<%foreach (var s in myStringArray){%>'<%=s%>',<%}%>];
Adrian Grigore
you have to get rid of the last comma
Omu
@Omu: Not necessarily. Javascript parsers are very forgiving in that area. All common browsers don't mind about trailing commas in arrays.
Adrian Grigore
@Adrian Grigore cool, just like in c#
Omu
+5  A: 

You could JSON serialize it. This way could could pass even more complex values and not worry about escaping simple quotes, double quotes, etc :

var categoriesList = <%= new JavaScriptSerializer().Serialize(new[] { "value1", "value2" }) %>;

Writing an HTML helper to do this would be even better:

public static class HtmlExtensions
{
    public static string JsonSerialize(this HtmlHelper htmlHelper, object value)
    {
        return new JavaScriptSerializer().Serialize(value);
    }
}

and then in your view:

<script type="text/javascript">
    var categoriesList = <%= Html.JsonSerialize(new[] { "value1", "value2" }) %>;
</script>
Darin Dimitrov
I have to admit it's a much cleaner approach than my answer. This should be voted as the solution, not my post.
Adrian Grigore
@Adrian, everything is subjective. Personally I hate seeing C# in views. And I am particularly allergic to loops in views :-) Also not encoding the strings your are outputting is extremely dangerous and subject to XSS attacks. That's why I prefer to always rely on things built into the framework so that I don't have to worry about properly escaping, ...
Darin Dimitrov