Selamlar ,
RestSharp kullanarak fake apiden Kendi apime verileri çektim sonrasında mvc den api deki verileri restsharp ile çekmeye çalıştım fakat View de listelemeye çalışırken hata alıyorum
buradan karışık oluyor özelden projeyi de gönderebilirim yardımcı olabilecek olana
public class ProductViewModel
{ public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public int price { get; set; }
public double discountPercentage { get; set; }
public double rating { get; set; }
public int stock { get; set; }
public string brand { get; set; }
public string category { get; set; }
public string thumbnail { get; set; }
public List<string> images { get; set; } }
}
public class RootViewModel
{ public List<ProductViewModel> Products { get; set; }
public int total { get; set; } public int skip { get; set; }
public int limit { get; set; } }- public interface IWebProductClient
- { Task<ProductViewModel> GetProductById(int id);
- Task<List<ProductViewModel>> GetAllProduct();
- Task<RootViewModel> Create(RootViewModel rootViewModel);
- Task<RootViewModel> Update(RootViewModel rootViewModel);
Task<ProductViewModel> Delete(int id); }
public class WebProductClient : IWebProductClient
{ private readonly RestClient _client;
private readonly string _url;
public WebProductClient()
{ _url = "https://localhost:7123";
var options = new RestClientOptions(_url);
_client = new RestClient(options);
public async Task<List<ProductViewModel>> GetAllProduct()
{ var request = new RestRequest("/api/Product");
var response = await _client.ExecuteGetAsync(request);
var value = JsonConvert.DeserializeObject<List<ProductViewModel>>(response.Content);
return value; } Controller
public IActionResult Index()
{ var all = _client.GetAllProduct();
return View(all); }
Example View Index
@using DummyShop.Service.ViewModel;
@model List<ProductViewModel>
@{ ViewData["Title"] = "Index"; }
<h1>Index</h1>
<p> <a asp-action="Create">Create New</a> </p>
<table class="table">
<tbody> @foreach (var item in Model)
{ <tr> <td> @Html.DisplayFor(modelItem => item.id) </td>
Json Data : https://dummyjson.com/products
Aldığım Hata :
Error : An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.Collections.Generic.List1[DummyShop.Service.ViewModel.ProductViewModel],System.Runtime.CompilerServices.IAsyncStateMachine]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List1[DummyShop.Service.ViewModel.ProductViewModel]'.