asp.net razor For Each语法

时间:2022-10-19 21:27:12

I have some asp.net razor syntax problems. I have this JavaScript array, want to convert it to razor syntax:

我有一些asp.net剃刀语法问题。我有这个JavaScript数组,想将其转换为razor语法:

var Vendors = [
          "ActionScript",
          "AppleScript",
          "Asp",
          "BASIC",
          "C"
                ];

To:

var Vendors = [
        @For Each Vendor In ViewBag.VendorsList

           @Vendor.Name + ","

        Next
        ];

But there is some errors, what is the right way? Thx a lot.

但是有一些错误,正确的方法是什么?多谢。

2 个解决方案

#1


4  

It's recommended to process your data elsewhere, the presentation layer should preferably do only one thing, the UI. If this array is needed on the client-side, then you'll have to return it from your C# code (server-side). Else if you have an array on the client-side and wish to do something with it, just post it to the server and do your processing there.

建议在其他地方处理您的数据,表示层最好只做一件事,UI。如果在客户端需要这个数组,那么你必须从C#代码(服务器端)返回它。否则,如果您在客户端有阵列并希望对其进行操作,只需将其发布到服务器并在那里进行处理。

It's not really clear what you're trying to accomplish here.

你在这里想要完成的事情并不是很清楚。

#2


0  

It looks like you have a list of values in a server-side variable and you want to inject that into the view's JS code as an array....

看起来您在服务器端变量中有一个值列表,并且您希望将其作为数组注入到视图的JS代码中....

so (I've coded this blind so it might need some tweaking)...

所以(我把这个盲人编码,所以可能需要一些调整)......

 @{
      List<script> mylist = ViewBag.VendorsList == null ? new List<script>() : (List<script>)ViewBag.VendorsList;
      string delim = "";
}


<script>
var vendors = [
     @foreach(var item in mylist) {
       string theOutput = delim + "\"" + item + "\"";
       delim = ","
     <text>
     @theOutput
     </text>

     }
];
</script>

#1


4  

It's recommended to process your data elsewhere, the presentation layer should preferably do only one thing, the UI. If this array is needed on the client-side, then you'll have to return it from your C# code (server-side). Else if you have an array on the client-side and wish to do something with it, just post it to the server and do your processing there.

建议在其他地方处理您的数据,表示层最好只做一件事,UI。如果在客户端需要这个数组,那么你必须从C#代码(服务器端)返回它。否则,如果您在客户端有阵列并希望对其进行操作,只需将其发布到服务器并在那里进行处理。

It's not really clear what you're trying to accomplish here.

你在这里想要完成的事情并不是很清楚。

#2


0  

It looks like you have a list of values in a server-side variable and you want to inject that into the view's JS code as an array....

看起来您在服务器端变量中有一个值列表,并且您希望将其作为数组注入到视图的JS代码中....

so (I've coded this blind so it might need some tweaking)...

所以(我把这个盲人编码,所以可能需要一些调整)......

 @{
      List<script> mylist = ViewBag.VendorsList == null ? new List<script>() : (List<script>)ViewBag.VendorsList;
      string delim = "";
}


<script>
var vendors = [
     @foreach(var item in mylist) {
       string theOutput = delim + "\"" + item + "\"";
       delim = ","
     <text>
     @theOutput
     </text>

     }
];
</script>