List<Student> studentsList = new List<Student>(); studentsList = GetStudents(); ViewBag.Students = System.Web.Helpers.Json.Encode(studentsList);We use System.Web.Helpers.Json.Encode() function to do the formatting and on the view, we just need to render the passed content like this:
@Html.Raw((String)ViewBag.Students)
A different way to do this is by passing the collection directly through ViewBag
ViewBag.Students = studentsList;then we do the formatting on the view
@Html.Raw(Json.Encode((IList<Student>)ViewBag.Students))However this way is less efficient than the earlier one.
No comments:
Post a Comment