简体   繁体   English

如何更改数据模型中项目的显示值?

[英]How to change display value of items in data model?

On the list of users, I would like to display the name of their role instead of roleId. 在用户列表上,我想显示其角色的名称,而不是roleId。 Currently the data is loaded in the following form: 当前,数据以以下形式加载:

@model IEnumerable<DemoRes.Models.User>

<div id="divUsers">
    <h2>Users</h2>
    <table class="table">
        <tr>
            <th>Username</th>
            <th>Email</th>
            <th>Access Role</th>
            <th>Enabled</th>
            <th></th>
        </tr>
        <tbody data-bind="foreach: List">
            <tr>
                <td data-bind="text : Username"></td>
                <td data-bind="text : Email"></td>
                <td data-bind="text : Role"></td>
                <td data-bind="text : IsActive"></td>
            <td>


</td>
            </tr>
        </tbody>
    </table>
</div>

The role property of User objects is an int value (0 - Admin, 1 - Member). User对象的role属性是一个int值(0-Admin,1-Member)。

Currently in the form thus in the column "Access Role", integer value is displayed. 当前以“访问角色”列的形式显示,整数值。

What I would like to do is replace the integer value with belonging string (either "Admin" or "Member") . 我想做的是用附属字符串(“ Admin”或“ Member”)替换整数值 Does anyone know what is the best way to accomplish that, given that the roles could be in general represented with arbitrary list or dictionary? 鉴于角色通常可以由任意列表或字典表示,有人知道实现此目标的最佳方法是什么? How should the list (or dictionary) of roles be passed to form in this case (for example, using ViewBag, by modifying view model...)? 在这种情况下,应如何将角色列表(或词典)传递给表单(例如,使用ViewBag,通过修改视图模型...)?

Thanks! 谢谢!

Best practice here is to put it in the view model, then you can reference it in your knockout: 此处的最佳实践是将其放入视图模型,然后可以在剔除中引用它:

public string RoleName { get; set; }

Then populate it with your data (in your controller/business logic/model population code), then you can do: 然后用您的数据(在控制器/业务逻辑/模型填充代码中)填充它,然后您可以执行以下操作:

<td data-bind="text : RoleName"></td>

As an aside from that, as @David mentioned, it looks like you're displaying passwords, which aside from the fact is terrible security , it also means that you're storing them unencrypted, which means that if someone gains access to your database, they get everyone's passwords, not good . 除此之外,就像@David提到的那样,您似乎在显示密码,除了安全性很差之外,这还意味着您未加密地存储了密码,这意味着如果有人可以访问您的数据库,他们得到每个人的密码, 不好

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

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