从Controller中绑定JSON数据并在View中显示名称

时间:2018-03-21 14:54:57

标签: json asp.net-mvc

开发人员工具网络标签中的我的JSON字符串是:

[{\"id\":\"904c7ccd-d568-40ca-a3ab-1d097d555f52\",\"name\":\"BPANDE2\",\"actualName\":\"Pande, Bhavana (B.V.)\",\"displayName\":\"Pande, Bhavana (B.V.) (BPANDE2)\",\"email\":\"bpande2@abc.com\",\"deleted\":false,\"deletedDate\":0,\"authenticationRealm\":\"f4191d4b-4331-4637-af6f-c89781687bf6\",\"isLockedOut\":false,\"lastLoginDate\":1521144701377,\"isDeletable\":true,\"groups\":[{\"id\":\"7862c275-8eb2-42cf-895c-214c2826a0b8\",\"name\":\"config_viewer\",\"enabled\":true}]}]

我的控制器代码是

public class Login
{
    public string id { get; set; }
    public string name { get; set; }
    public bool enabled { get; set; }
}
public class LoginDetails
{
    public string id { get; set; }
    public string name { get; set; }
    public string actualName { get; set; }
    public string displayName { get; set; }
    public string email { get; set; }
    public bool deleted { get; set; }
    public int deletedDate { get; set; }
    public string authenticationRealm { get; set; }
    public bool isLockedOut { get; set; }
    public long lastLoginDate { get; set; }
    public bool isDeletable { get; set; }
    public List<Login> groups { get; set; }
}

public ActionResult Index()
{
  //Covert JSON to .net object using Deserialize method
            var jsonString = **The json string is displayed as above in "my json string in developer tool network tab is:"**
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    List<LoginDetails> listName = (List<LoginDetails>)javaScriptSerializer.Deserialize(jsonString,typeof(List<LoginDetails>)); 

    foreach (LoginDetails login in listName)
    {
        Response.Write("Welcome" + "<br/>");
        Response.Write("" + login.displayName + "<br/>");
    }
}

我的观看代码是:

@{
    ViewBag.Title = "Index";
}
Welcome **@Model.displayName**

我想在View中显示displayName,但“@ Model.displayName”似乎不起作用。你能帮我解决一下如何使用ViewData属性在View中显示名称?或者可能是视图中的其他属性?

1 个答案:

答案 0 :(得分:0)

嗯,那不是你应该如何使用MVC。

您的行动Indexreturn View(model);,以便您的观点能够正确呈现。并且您的View应该说明模型的类型。

在ASP.Net MVC中,直接写入响应流并且在这里肯定是错误的是不常见的。

Index更改为

public ActionResult Index()
{
    var jsonString = « The JSON string from below »
    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    List<LoginDetails> listName = (List<LoginDetails>)javaScriptSerializer.Deserialize(jsonString,typeof(List<LoginDetails>)); 

    return View(listName.First());
}

同时将您的视图更改为

@model LoginDetails
@{
    ViewBag.Title = "Index";
}

Welcome @Model.displayName

作为参考,我已经在这里添加了格式化的JSON

[
  {
    "id": "904c7ccd-d568-40ca-a3ab-1d097d555f52",
    "name": "BPANDE2",
    "actualName": "Pande, Bhavana (B.V.)",
    "displayName": "Pande, Bhavana (B.V.) (BPANDE2)",
    "email": "bpande2@abc.com",
    "deleted": false,
    "deletedDate": 0,
    "authenticationRealm": "f4191d4b-4331-4637-af6f-c89781687bf6",
    "isLockedOut": false,
    "lastLoginDate": 1521144701377,
    "isDeletable": true,
    "groups": [
      {
        "id": "7862c275-8eb2-42cf-895c-214c2826a0b8",
        "name": "config_viewer",
        "enabled": true
      }
    ]
  }
]