I have a project using MVC4, I want to ask how to get data from webapi and return to the view.
Model
public class Name { public Int32 NameId { get; set; } public String FirstName{ get; set; } public String LastName{ get; set; } public String CreatedBy { get; set; } } public class IListMyProject { public List<Name> Names { get; set; } }
I can list everything in my Index.cshtml with this code
public ActionResult Index() { string securityToken = repo.GetTokens(); if (securityToken != null) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "webapiurl/api/Name/Get?$orderby=LastName&$top=10"); string authHeader = System.Net.HttpRequestHeader.Authorization.ToString(); httpRequestMessage.Headers.Add(authHeader, string.Format("JWT {0}", securityToken)); var response = client.SendAsync(httpRequestMessage) .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()) .Result; if (response.IsSuccessStatusCode) { model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList(); } } return View("Index", model); }
I can get my opinion back. and now I have another view called Details.cshtml with this code:
public ActionResult Details(string id) { string securityToken = repo.GetTokens(); if (securityToken != null) { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "webapiurl/api/Name/GetById/"+id+""); string authHeader = System.Net.HttpRequestHeader.Authorization.ToString(); httpRequestMessage.Headers.Add(authHeader, string.Format("JWT {0}", securityToken)); var response = client.SendAsync(httpRequestMessage) .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()) .Result; if (response.IsSuccessStatusCode) { model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList(); } } return View(model); }
For this detail, my Json looks like this:
application/json, text/json { "NameId": 1, "FirstName": "This is First Name", "LastName": "This is Last Name", "CreatedBy": "This is Created By" }
when I run it, I get this error:
Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.IList`1[Models.Name]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'NameId', line 1, position 10.
How to fix this, I'm new to webapi. I wonder why if I list everything (for the index, I use api / get), it works, but when I want to show it in detail, it does not work.
thank for help
Hi
EDIT
when i debug
model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList();
Null tells him, is there something wrong when I try to get an answer?