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; }
}
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
39 Points
63 Posts
Display deserialized json data in View
Oct 15, 2019 11:17 AM|LetMeCode|LINK
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.
and in my View, I have this :
All-Star
53051 Points
23634 Posts
Re: Display deserialized json data in View
Oct 15, 2019 11:28 AM|mgebhard|LINK
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
All-Star
40565 Points
6233 Posts
Microsoft
Re: Display deserialized json data in View
Oct 16, 2019 01:49 AM|Fei Han - MSFT|LINK
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.
With Regards,
Fei Han
All-Star
58214 Points
15668 Posts
Re: Display deserialized json data in View
Oct 16, 2019 09:00 PM|bruce (sqlwork.com)|LINK
You want to deserialize the json to an object,then pass the object to the view: