首页 > 解决方案 > 创建灵活的模型

问题描述

在我的 WebApp 中,我使用了一个 wcf Web 服务,在我的示例中,我使用了一个返回超过 400 个值类型的方法。首先,WebApp 将在 View(Table) 中显示两列

           //Model
           public class OrdersviewModel
           {
             public int Articlenr { get; set; }
             public string ArticleName { get; set; }
           } 

后来用户有可能添加或显示新列值,问题是模型如何?

标签: c#asp.net-core-mvc

解决方案


那是你的模型:

public class MyViewModel
{    
    public Dictionary<int, string> OrdersViewDictionary { get; set; }       
}

进入视图,您可以使用这样的字典

  @model MyViewModel

<table class="table-bordered">
    <thead>
        <tr>            
            <th>Index key</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>

            @foreach (var pairData in Model.OrdersViewDictionary) {
                <tr>

                    <td>@pairData.Key</th>
                    <td>@pairData.Value</th>

                </tr>
            }

    </tbody>
</table>

推荐阅读