简体   繁体   English

GroupBy Knockout.js

[英]GroupBy Knockout.js

I have this Razor code in C# and try to do the same in Knockout 我在C#中使用这个Razor代码并尝试在Knockout中执行相同的操作

 @foreach (var group in Model.Details.SonstigeNummern.OeReferenznummern.GroupBy(x => x.Hersteller))
                {
                    <tr class="ListItem @oddeven">
                        <td>@group.Key</td>
                        <td>
                            @foreach (var nummer in group)
                            { 
                                @nummer.Nummer<br />
                            }
                        </td>
                    </tr>
                }

I have no clue how i could manage the GroupBy. 我不知道如何管理GroupBy。

My Viewmodel has the same Syntax as my c# model 我的Viewmodel与我的c#模型具有相同的语法

Can Someone help with this? 有人可以帮忙吗?

Sincerly 诚恳

I am not sure about the linq query. 我不确定linq查询。 But I think you should do something like this. 但我认为你应该这样做。 As you can see I just convert all data into arrays and then into JSON string. 如您所见,我只是将所有数据转换为数组,然后转换为JSON字符串。 And on this client side I re-convert them into javascript Object. 在这个客户端,我将它们重新转换为javascript对象。

<table data-bind="foreach: groups">
     <tr class="ListItem @oddeven" >
        <td data-bind="value: Key"></td>
        <td data-bind="foreach: Nummers">
            <span data-bind="text: $data"></span>
        </td>
    </tr>
 </table>



<script type="text/javascript">
    var data= JSON.parse('@Html.Raw(Json.Encode(Model.Details.SonstigeNummern.OeReferenznummern.GroupBy(x => x.Hersteller).Select( g => new {Key = g.Key, Nummers =  g.ToArray()}).ToArray()))');
    var vm = { groups: data};
    ko.applyBindings(vm);
</script>

I hope it helps. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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