asp.net mvc - Initializing a JavaScript array in Razor -
I have an ASP.NET MVC app that uses the razor in the views. I am creating a JavaScript array in my controller sometimes, though, it will not be in existence. For this reason, I want to start it on client-side. In an effort to do this, I have the following:
var list = '@ (viewback_list == empty? []: See bag.list)'; Unfortunately, this is an error that generates an error: Compilation Error Description: An error during the collection of the required resources for the service of this request. Hui. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS 1525: Invalid expression word '[' Is there a way to use a ternary operator with arrays in the razor? If so, then how?
Because JavaScript is running using JSON, you should serial JSON in your list: < / P> @Nutonsoft Jason Var list = @html. Ro (Jason Convert. SerializeObject (ViewBag.List)); List = list || [];
In general, @ Html.Raw (JsonConvert.SerializeObject (obj)) is the correct way to inject a C # object obj In JSON as a view, IHtmlString Html.Raw (String) returns just string and gives it without avoiding any HTML, which is when you use any A block of JavaScript code that provides objects as JSON
Comments
Post a Comment