6

I want to convert a Model property of type List to a Javascript variable usable in that same view. This is my model structure:

 public string Title { get; set; }
 public string Description { get; set; }
 public List<String> ImgLinks { get; set; }

I want a Javascript array or json of the ImgLinks property of my model. I have tried-

var imageLinks = @(Html.Raw(Json.Encode(Model.ImgLinks)));

But i get a syntax error warning. Can anyone please help me out with the conversion to both javascript array and json?

Fabio
12k1 gold badge29 silver badges42 bronze badges
asked Dec 18, 2015 at 10:46
10
  • 1
    Which warning do you get? Commented Dec 18, 2015 at 10:48
  • The warning is syntax error Commented Dec 18, 2015 at 10:49
  • 1
    try JSON.parse('@Html.Raw(Json.Encode(Model.ImgLinks))'); Commented Dec 18, 2015 at 10:51
  • What does this line in the rendered page actually look like? Commented Dec 18, 2015 at 10:52
  • Now, i get no syntax error warning. I think i tried this earlier but i forgot that JSON.parse takes a string as parameter. Thanks @ShekharPankaj Commented Dec 18, 2015 at 10:54

2 Answers 2

6

To get rid of the 'syntax error' you just need to remove ; at the end

var imageLinks = @Html.Raw(Json.Encode(Model.ImgLinks))

Despite the warning your code will run fine anyway.

Here's a different kind of solution to the problem if anyone's interested. You can loop through the razor collection and store the values in a Javascript Array like this.

<script type="text/javascript">
 var myArray = [];
 @foreach (var link in Model.ImgLinks)
 {
 @:myArray.push("@link");
 }
</script>
answered Dec 18, 2015 at 10:58

6 Comments

what does the : before myArray mean? Is it a way to reference a javascript variable in razor for loop?
It says "this line is not executed as C#", and should be emitted (in this case that is JS) after replacements. It might not necessarily be variable.
learn.microsoft.com/en-us/aspnet/web-pages/overview/… "The @: outputs a single line of content containing plain text or unmatched HTML tags.." — so @: starting a line is pretty much the opposite of @ starting a line.
I never said it did not. I was correcting the incorrect assertion that @: had anything to do with variables. It does not. It relates to text emitted in the output.
I didn't read the edits you made later. Your comments didn't have all that information before. Of course you're right :)
|
1

There is a more elegant solution. You need to serialize your list to JSON:

var imageLinks = @Json.Encode(Model.ImgLinks); // <- annoying syntax error

So you did it right. Now to fix this syntax error I would suggest you to use simple javascript set function:

function set(value) {
 return value;
}
var imageLinks = set(@Json.Encode(Model.ImgLinks));

You can find more info about fixing this syntax error here.

answered Dec 18, 2015 at 13:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.