locked
Display deserialized json data in View RRS feed

  • Question

  • User1242168447 posted

    I'm still unable to display data in View. I'm only getting a null result in View page. When I debug using a break point, I can clearly see data in the variables but can't return it in View. Seems like I need a List...
    The aim is to return json data in a HTML View.

    public async Task<ActionResult> GetAPIStringAsync(Students model)
            {
    
                HttpClient client = new HttpClient();
    
                string APIdatas = null;
    
                HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/");
                if (response.IsSuccessStatusCode)
                {
                    APIdatas = await response.Content.ReadAsStringAsync();
                }
    
    
                var stringJson = JsonConvert.DeserializeObject<IEnumerable<Students>>(APIdatas);
    
                return Json(model, JsonRequestBehavior.AllowGet);
                return View();
    
            }
    
    
    public class Students
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Url { get; set; }
    }

    and in my View, I have this :

    @model IEnumerable<AutoMobile.Web.Models.Manage.Students>
    
    @foreach (var item in Model.OrderBy(x => x.Id))
    {
    
    <td>
        @Html.DisplayFor(modelItem => item.Id)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Url)
    </td>
    
    
    }

    Tuesday, October 15, 2019 11:17 AM

Answers

  • User61956409 posted

    Hi LetMeCode,

    If you'd like to consume API to get json data and deserializes it to the specified .NET type, then pass data from controller and display it in view, you can modify the code like below.

    public async Task<ActionResult> GetAPIStringAsync(Students model)
    {
        //code logic here
    
        var stus = JsonConvert.DeserializeObject<IEnumerable<Students>>(APIdatas);
    
        return View(stus);
    }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 1:49 AM
  • User-474980206 posted

    You want to deserialize the json to an object,then pass the object to the view:

     var model = JsonConvert.DeserializeObject<IEnumerable<Students>>(APIdatas);
     return View(model);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 9:00 PM

All replies

  • User475983607 posted

    In C# a method can return one type.  It is not possible to return JSON and a View (HTML).  Secondly, A View returns HTML not JSON.  I recommend going through a few Getting Started tutorials.https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started

    Tuesday, October 15, 2019 11:28 AM
  • User61956409 posted

    Hi LetMeCode,

    If you'd like to consume API to get json data and deserializes it to the specified .NET type, then pass data from controller and display it in view, you can modify the code like below.

    public async Task<ActionResult> GetAPIStringAsync(Students model)
    {
        //code logic here
    
        var stus = JsonConvert.DeserializeObject<IEnumerable<Students>>(APIdatas);
    
        return View(stus);
    }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 1:49 AM
  • User-474980206 posted

    You want to deserialize the json to an object,then pass the object to the view:

     var model = JsonConvert.DeserializeObject<IEnumerable<Students>>(APIdatas);
     return View(model);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 16, 2019 9:00 PM