将JSON数据绑定到表MVC 4中的行

时间:2018-04-30 15:30:30

标签: json asp.net-mvc rest

我需要帮助构建Controller中的代码,以将JSON数据绑定到表中的行。以下是Controller

的代码
var jsonString = "{\"roleMappings\":[{\"user\":{\"id\":\"4b147d87-64eb-4049-bbd3-ade96d18af89\",\"name\":\"PFRENC17\",\"actualName\":\"French, Phil (P.)\",\"displayName\":\"French, Phil (P.) (PFRENC17)\",\"email\":\"pfrenc17@abc.com\",\"deleted\":false,\"deletedDate\":0,\"authenticationRealm\":\"f4191d4b-4331-4637-af6f-c89781687bf6\",\"isLockedOut\":false,\"lastLoginDate\":1502803623890,\"isDeletable\":true},\"role\":{\"id\":\"3de854ce-34ed-4b7f-9b99-9fdf04c0345c\",\"name\":\"Developer\",\"description\":\"Application Developer\",\"isDeletable\":true}},{\"user\":{\"id\":\"3afd930e-9608-4257-a962-0a82b792a6e9\",\"name\":\"RSIEGLE3\",\"actualName\":\"Siegler, Ryan (R.)\",\"displayName\":\"Siegler, Ryan (R.) (RSIEGLE3)\",\"email\":\"rsiegle3@abc.com\",\"deleted\":false,\"deletedDate\":0,\"authenticationRealm\":\"f4191d4b-4331-4637-af6f-c89781687bf6\",\"isLockedOut\":false,\"lastLoginDate\":1500300166062,\"isDeletable\":true},\"role\":{\"id\":\"3de854ce-34ed-4b7f-9b99-9fdf04c0345c\",\"name\":\"Developer\",\"description\":\"Application Developer\",\"isDeletable\":true}},{\"user\":{\"id\":\"33dd9fea-4685-4505-91ba-5c0508301b56\",\"name\":\"yyang4\",\"actualName\":\"Yang, Yvonne (Y.)\",\"displayName\":\"Yang, Yvonne (Y.) (yyang4)\",\"email\":\"yyang4@abc.com\",\"deleted\":false,\"deletedDate\":0,\"authenticationRealm\":\"f4191d4b-4331-4637-af6f-c89781687bf6\",\"isLockedOut\":false,\"lastLoginDate\":1522249840141,\"isDeletable\":true},\"role\":{\"id\":\"f722b338-01c1-43cb-bc53-853cfb0d2249\",\"name\":\"UC Auto Discovery\",\"description\":\"\",\"isDeletable\":false}},{\"user\":{\"id\":\"33dd9fea-4685-4505-91ba-5c0508301b56\",\"name\":\"yyang4\",\"actualName\":\"Yang, Yvonne (Y.)\",\"displayName\":\"Yang, Yvonne (Y.) (yyang4)\",\"email\":\"yyang4@abc.com\",\"deleted\":false,\"deletedDate\":0,\"authenticationRealm\":\"f4191d4b-4331-4637-af6f-c89781687bf6\",\"isLockedOut\":false,\"lastLoginDate\":1522249840141,\"isDeletable\":true},\"role\":{\"id\":\"8421e59e-ce5f-477b-aa6f-0313aa1223e6\",\"name\":\"Application_Lead\",\"description\":\"Development Team Lead\",\"isDeletable\":true}},{\"user\":{\"id\":\"5599437f-68f9-4c20-bd0a-5c26d55628e1\",\"name\":\"UBLDUCD1\",\"actualName\":\"Blducd1, U (U.)\",\"displayName\":\"Blducd1, U (U.) (UBLDUCD1)\",\"deleted\":false,\"deletedDate\":0,\"authenticationRealm\":\"f4191d4b-4331-4637-af6f-c89781687bf6\",\"isLockedOut\":false,\"lastLoginDate\":1515694447960,\"isDeletable\":true},\"role\":{\"id\":\"8421e59e-ce5f-477b-aa6f-0313aa1223e6\",\"name\":\"Application_Lead\",\"description\":\"Development Team Lead\",\"isDeletable\":true}},{\"group\":{\"id\":\"7862c275-8eb2-42cf-895c-214c2826a0b8\",\"name\":\"config_viewer\",\"enabled\":true},\"role\":{\"id\":\"ba4cdbf1-6b97-436f-9b40-a7e21d8394f2\",\"name\":\"ReadOnly\",\"description\":\"Read-only access\",\"isDeletable\":true}}],\"id\":\"877eb9b9-6dd9-432d-b55a-01bd406d039a\",\"name\":\"AftImport Team\",\"isDeletable\":true}";
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                List<Dictionary<string,User>> rows = (List<Dictionary<string,User>>)javaScriptSerializer.Deserialize(jsonString, typeof(Dictionary<string,User>));
                foreach (Dictionary<string,User> teams in rows)
                {
                    rows = new Dictionary<string, object>();
                    foreach(DataColumn col in column)
                    row.add();
                }

以下是View

的代码
<table border="1" align="center" style="font-family:Arial; width:900px;">
        <tr height="50">
            <th>Member</th> <th>Roles</th> <th>Actions</th>
        </tr>
        <tr height="30">
            <th></th>
            <th></th>
            <th><button class="editbtn">Delete User</button>&nbsp;&nbsp;<button class="editbtn">Change Role</button></th>
        </tr>
        <tr height="30">
            <th></th>
            <th></th>
            <th></th>
        </tr>
        <tr height="30">
            <th></th>
            <th></th>
            <th></th>
        </tr>

    </table>

我需要帮助绑定&#34;名称:PFRENC17&#34;领域&#34;会员&#34;在View.So我将能够在表中的成员行中显示所有名称。

1 个答案:

答案 0 :(得分:0)

鉴于您想要执行此服务器端,这是使用Razor视图引擎的简单情况。例如:

模特:

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string DisplayName { get; set; }
    public Role Role { get; set; }
}

查看:

@model Dictionary<string, User>

<table border="1" align="center" style="font-family:Arial; width:900px;">
    <tr height="50">
        <th>Member</th>
        <th>Roles</th>
        <th>Actions</th>
    </tr>

    @foreach (var user in Model.Select(x => x.Value))
    {
        <tr height="30">
            <th>@user.DisplayName</th>
            <th>@user.Role.Name</th>
            <th><button class="editbtn">Delete User</button>&nbsp;&nbsp;<button class="editbtn">Change Role</button></th>
        </tr>
    }
</table>