简体   繁体   中英

Convert in JavaScript C# code that reads from web.config

in the web.config file of my ASP.NET MVC application I have some constant string values. sommething like this:

<add key="filters" value="AA;BB;CC;DD" />

In a C# file I create a List in this way:

List<string> filters = new List<string>(ConfigurationManager.AppSettings["filter"].Split(new char[] { ';' }));

Now I have to put this code in a JavaScript file and create as many string variables as strings are present in this List.

For example, in this case I should obtain:

filterAA filterBB filterCC filterDD

How can I accomplish this task?

Thank you in advance.

Luis

You can add List to a view model in a controller, then you sent the data to View via ViewModel. Add this code inside of View

@model ViewModel

... your html code

<script>

   var filters = @Html.Raw(Json.Serialize(@Model.Filters));
 
 </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM