每次运行我的应用程序时,我都有一个问题,它给出了这个错误.我通过行来调试它,并看到错误在执行行时打开的错误=等待客户机.sendasync(req); 我已经试过了,我找不到解决办法.我在任何地方搜索过,我从“SendAsync”变成了所有可能的事情,我无法解决它,我已经尝试了2天了. 我的代码: 我的索引(视图): 我的界面: 编辑1: 错误:ERRO 编辑2(控制器): 我的类产品: 您必须获取数据以查看您的视图模型应该是这样的 与控制器 列名的lambda表达式应该是

提问

每次运行我的应用程序时,我都有一个问题,它给出了这个错误。我通过行来调试它,并看到错误在执行行时打开的错误=等待客户机。sendasync(req); 我已经试过了,我找不到解决办法。我在任何地方搜索过,我从“SendAsync”变成了所有可能的事情,我无法解决它,我已经尝试了2天了。 我的代码: 我的索引(视图): 我的界面: 编辑1: 错误:ERRO 编辑2(控制器): 我的类产品: 您必须获取数据以查看您的视图模型应该是这样的 与控制器 列名的lambda表达式应该是

public async Task ListAsync()
        {
            var client = new HttpClient
            {
                BaseAddress = new Uri("https://teste.teste1.com.br")
            };
            client.DefaultRequestHeaders.Add("x-user-email", "victor@teste.com.br");
            client.DefaultRequestHeaders.Add("x-api-key", "eyJ0eXAiOiJKV1QiLCJhbGcxYg");
            client.DefaultRequestHeaders.Add("x-store-key", "1");
            client.DefaultRequestHeaders.Add("accept", "application/json;charset=UTF-8");
           // client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, "/app/Api/V1/Products/list");
            req.Content = new StringContent("Content-Type", Encoding.UTF8, "application/json");
            using var res = await client.SendAsync(req);
            res.EnsureSuccessStatusCode();
            var responseBody = await res.Content.ReadAsStringAsync();
            var root = JsonSerializer.Deserialize<Product>(responseBody);
        }

@model IEnumerable<WebConsumoApi.Models.Product>
@{
    ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.sku)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.status)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.qty)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.list_price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.weight_gross)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.weight_liquid)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.height)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.width)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.length)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.items_per_package)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.brand)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ean)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.sku)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.status)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.qty)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.list_price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.weight_gross)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.weight_liquid)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.height)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.width)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.length)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.items_per_package)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.brand)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ean)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.sku }) |
                @Html.ActionLink("Details", "Details", new { id = item.sku }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.sku })
            </td>
        </tr>
}
    </tbody>
</table>

using WebConsumoApi.Models;
namespace WebConsumoApi.Interfaces
{
    public interface IProduto
    {
        Task ListAsync();
        Product Create(Product produto);
        Product Update(Product produto);
        Product GetOne(string Codigo);
        Product Delete(string Codigo);
    }
}

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebConsumoApi.Interfaces;
using WebConsumoApi.Models;
namespace WebConsumoApi.Controllers
{
    public class ProdutosController : Controller
    {
        private readonly IProduto _IProduto;
        public ProdutosController(IProduto IProduto)
        {
            _IProduto = IProduto;
        }
        // GET: ProdutosController
        public async Task<ActionResult> Index()
        {
            return View(_IProduto.ListAsync());
        }
        // GET: ProdutosController/Details/5
        public ActionResult Details(string id)
        {
            return View(_IProduto.GetOne(id));
        }
        // GET: ProdutosController/Create
        public ActionResult Create()
        {
            return View();
        }
        // POST: ProdutosController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Product collection)
        {
            try
            {
                _IProduto.Create(collection);
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
        // GET: ProdutosController/Edit/5
        public ActionResult Edit(string id)
        {
            return View(_IProduto.GetOne(id));
        }
        // POST: ProdutosController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, Product collection)
        {
            try
            {
                _IProduto.Update(collection);
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
        // GET: ProdutosController/Delete/5
        public ActionResult Delete(string id)
        {
            return View(_IProduto.GetOne(id));
        }
        // POST: ProdutosController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(string id, Product collection)
        {
            try
            {
                _IProduto.Delete(id);
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}

namespace WebConsumoApi.Models
{
    public class Product
    {
        public string sku { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string status { get; set; }
        public int qty { get; set; }
        public float price { get; set; }
        public float? list_price { get; set; }
        public float weight_gross { get; set; }
        public float weight_liquid { get; set; }
        public int height { get; set; }
        public int width { get; set; }
        public int length { get; set; }
        public string items_per_package { get; set; }
        public string brand { get; set; }
        public string ean { get; set; }
        public object ncm { get; set; }
        public Category[] categories { get; set; }
        public string[] variation_attributes { get; set; }
        public Variation[] variations { get; set; }
        public string[] images { get; set; }
        public Published_Marketplace published_marketplace { get; set; }
        public Marketplace_Offer_Links[] marketplace_offer_links { get; set; }
    }
}
回答

@model List<WebConsumoApi.Models.Product>

        private readonly DbContext _context;
        public ProdutosController( DbContext context)
        {
           _context=context;
        }
        public async Task<ActionResult> Index()
        { 
           List<Product> model= await _context.Products.ToListAsync();
            return View(viewModel);
        }

             <th>
                @Html.DisplayNameFor(model => model[0].sku)
            </th>
            <th>
                @Html.DisplayNameFor(model => model[0].name)
            </th>
            ... and so on

▼版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说cuoshuo.com——程序员的报错记录

部分内容根据CC版权协议转载,如果您希望取消转载请发送邮件到cuoshuo8@163.com

辽ICP备19011660号-5