如何解析Json数据并将其显示在MVC的视图中?

时间:2015-02-11 06:40:25

标签: json view telerik asp.net-mvc-5

我想使用Telerik的KendoUI Grid在我的视图中显示一些数据。像Kendo Grid这样的部分只接受Json Input。所以我试图在我的视图中返回JSON数据。这是我的代码: 我的控制器:

public class UserController : Controller
    {
        //
        // GET: /User/
        public JsonResult DisplayUserDetails()
        {
            List<UserModel> userList = new List<UserModel>();
            userList.Add(new UserModel
            {
                FirstName="Pawan",
                LastName="Kapoor",
                Address="Mohali",
                Email="test@test.com",
                Gender = "Male",               
                Salary=25000
            }

                );

            userList.Add(new UserModel
            {
                FirstName = "Ayan",
                LastName = "Banerjee",
                Address = "Bangalore",
                Email = "test1@test.com",
                Gender="Male",
                Salary = 30000
            }

                );
            userList.Add(new UserModel
            {
                FirstName = "Palak",
                LastName = "Thakur",
                Address = "Bangalore",
                Email = "test2@test.com",
                Gender = "Female",                
                Salary = 22000
            }

                );
            //var jsonSerializer = new JavaScriptSerializer();
            //var json=jsonSerializer.Serialize(userList);
            ViewBag.Response = Json(JsonConvert.SerializeObject(userList), JsonRequestBehavior.AllowGet);
            return ViewBag.Response;
        }

My Model

 public class UserModel
    {      
        [DisplayName("First Name")]
        [Required(ErrorMessage="First Name is Required")]
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Address { get; set; }

        [Required]
        [StringLength(50)]
        public string Email { get; set; }

        public string Gender { get; set; }

        [Range(100,1000000)]
        public decimal Salary {get;set; }

    }
 和我的观点:

<table>
    <tr>
        <th>
            FirstName
        </th>
        <th>
            LastName
        </th>
        <th>
            Address
        </th>
        <th>
            Email
        </th>
        <th>
            Gender
        </th>
        <th>
            Salary
        </th>
        <th></th>
    </tr>

    @{
        var json = ViewBag.Response;
    }
    @foreach (var item in json)
    {
        <tr>
            <td>
                @item.FirstName
            </td>
            <td>
                @item.LastName
            </td>
            <td>
                @item.Address
            </td>
            <td>
                @item.Email
            </td>
            <td>
                @item.Gender
            </td>
            <td>
                @item.Salary
            </td>
        </tr>
    }

</table>

我在浏览器中收到以下输出:

  

[{\ “姓\”:\ “爬完\” \ “名字\”:\ “卡普尔\”,\ “地址\”:\ “莫哈里\” \ “电子邮件\”:\“测试@ test.com \ “\ ”性别\“:\ ”男\“,\ ”工资\“:25000.0},{\ ”姓\“:\ ”阿燕\“ \ ”名字\“:\” 纳吉\”,\ “地址\”:\ “班加罗尔\”,\ “电子邮件\”:\ “test1@test.com \”,\ “性别\”:\ “男\”,\ “工资\”:30000.0 },{\ “姓\”:\ “PALAK \”,\ “名字\”:\ “塔库尔\”,\ “地址\”:\ “班加罗尔\”,\ “电子邮件\”:\“TEST2 @测试.COM \ “\ ”性别\“:\ ”女性\“,\ ”工资\“:22000.0}]”

它是字符串格式,我希望它在JSON中。我哪里错了?请建议

0 个答案:

没有答案