Full error
The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`
I'm trying to get some data from web api with viewmodel.
This is my action
public ActionResult PayBilgileri()
{
Session["dersGrupID"] = 470122;
int DersGrupID = Convert.ToInt32(Session["dersGrupID"]);
Core.ViewModel.Ders.DersDetay.PayVeNotBilgileriViewModel PayBilgileri = new Core.ViewModel.Ders.DersDetay.PayVeNotBilgileriViewModel();
PayBilgileri.DersPlanID = DersGrupID;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://myapi.com");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//HttpResponseMessage response = client.GetAsync("api/Ders/ListelePaylarVeNotBilgileri").Result;
var response = client.PostAsJsonAsync("api/Ders/ListelePaylarVeNotBilgileri", PayBilgileri).Result;
if (response.IsSuccessStatusCode)
{
}
return PartialView(response);
// PartialViewResult(response, JsonRequestBehavior.AllowGet);
}
}
And the partial view
@model IEnumerable<Core.ViewModel.Ders.DersDetay.PayVeNotBilgileriViewModel>
<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.KatkiOrani)
</td>
</tr>
}
</table>
You need to extract the data from the HttpResponseMessage
as IEnumerable<PayVeNotBilgileriViewModel>
and only then pass it as the view-model:
if (response.IsSuccessStatusCode)
{
var vm = response.Content.ReadAsAsync<IEnumerable<PayVeNotBilgileriViewModel>>().Result;
return PartialView(vm);
}
See ReadAsAsync
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments