【问题标题】:My MVC is not displaying the correct view when calling MakeRequest Method调用 MakeRequest 方法时,我的 MVC 未显示正确的视图
【发布时间】:2019-03-22 23:51:35
【问题描述】:

我有以下课程:

public class Product
{
        [JsonProperty("image")]
        public string ImageURL { get; set; }
        [JsonProperty("superDepartment")]
        public string SuperDepartment { get; set; }
        [JsonProperty("tpnb")]
        public long TPNB { get; set; }
        [JsonProperty("ContentsMeasureType")]
        public string ContentsMeasureType { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("UnitOfSale")]
        public int UnitOfSale { get; set; }
        [JsonProperty("description")]
        public IEnumerator<string> LstDescription { get; set; }
        public string Description { get; set; }
        [JsonProperty("AverageSellingUnitWeight")]
        public decimal AverageSellingUnitWeight { get; set; }
        [JsonProperty("UnitQuantity")]
        public string UnitQuantity { get; set; }
        [JsonProperty("id")]
        public long ID { get; set; }
        [JsonProperty("ContentsQuantity")]
        public int ContentsQuantity { get; set; }
        [JsonProperty("department")]
        public string Department { get; set; }
        [JsonProperty("price")]
        public decimal Price { get; set; }
        [JsonProperty("unitprice")]
        public decimal UnitPrice { get; set; }
    }

我在productController上有方法:

    public async Task<ActionResult> MakeRequest(string q)// worked 
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "56ac439a92694577a2779f3d0ee0cd85");

        var uri = string.Format("https://dev.tescolabs.com/grocery/products/?query={0}&offset={1}&limit={2}", q, 0, 10);

        var response = await client.GetAsync(uri);          
        string body = await response.Content.ReadAsStringAsync();
        Product myoutput = JsonConvert.DeserializeObject<Product>(body);
        }
        return View(body);
    }

我有下面的模型:

由于某种原因,当我调用 MakeRequest 方法时,它只将信息显示为字符串,并显示服务器错误,如下图所示,我们可以从 api 中看到信息,但它显示为字符串。 error message 来自 api 的信息应显示在下表中: table

如何在表格中显示数据?或者更好的如何将 json 数组转换为 net 对象?

我知道我的方法下面的部分缺少一些东西:

Product myoutput = JsonConvert.DeserializeObject<Product>(body);

【问题讨论】:

  • 您的模型与响应 json 模型不匹配。
  • 你好,Sinan,非常感谢你能给我更多关于模型模型不匹配响应 json 的信息吗?

标签: c# .net json api model-view-controller


【解决方案1】:

您将 json 字符串传递给您的视图,而不是您的反序列化产品。 我假设您的 View 需要 Product 类型的模型。

MakeRequest动作的return语句应该是return View(myOutput);

【讨论】:

  • 非常感谢 Vilius,
【解决方案2】:

我会用简单的方式告诉你。

你的 json 模型

-英国 --ghs - -产品 ----结果

{
  "uk" : {
    "ghs" : {
      "products": {
        "input_query": "babana",
        "output_query": "banana",
        "filters": {},
        "queryPhase": "post_primary",
        "totals": {
          "all": 109,
          "new": 1,
          "offer": 47
        },
        "config": "default",
        "results": [
          {
            "image": "http://img.tesco.com/Groceries/pi/000/0261480000000/IDShot_90x90.jpg",
            "superDepartment": "Fresh Food",
            "tpnb": 50502269,
            "ContentsMeasureType": "KG",
            "name": "Tesco Bananas Loose",
            "UnitOfSale": 3,
            "description": [ "To help ensure the banana farms we source from are best in class we, and our", "suppliers, now work closely with the Rainforest Alliance, an international", "non profit organisation that works to conserve biodiversity and ensure", "sustainable livelihoods worldwide.", "The Rainforest Alliance Certified&#8482; seal on our bananas   a green frog nestled", "in a circle &#8211; helps us to tell the story of how they are sustainably sourced,", "and assure customers we are sourcing from responsibly managed farms." ],
            "AverageSellingUnitWeight": 2.402,
            "UnitQuantity": "KG",
            "id": 275280804,
            "ContentsQuantity": 1,
            "department": "Fresh Fruit",
            "price": 0.76,
            "unitprice": 0.76
          },
          {
            "image": "http://img.tesco.com/Groceries/pi/875/0000010001875/IDShot_90x90.jpg",
            "superDepartment": "Fresh Food",
            "tpnb": 65728590,
            "ContentsMeasureType": "SNGL",
            "name": "Tesco Ripe Bananas 5 Pack",
            "UnitOfSale": 1,
            "AverageSellingUnitWeight": 0.862,
            "UnitQuantity": "EACH",
            "id": 266419328,
            "ContentsQuantity": 5,
            "department": "Fresh Fruit",
            "price": 0.9,
            "unitprice": 0.18
          },
          {
            "image": "http://img.tesco.com/Groceries/pi/416/0000003251416/IDShot_90x90.jpg",
            "superDepartment": "Fresh Food",
            "tpnb": 77427870,
            "ContentsMeasureType": "SNGL",
            "name": "Tesco Small Bananas 6 Pack",
            "UnitOfSale": 1,
            "description": [ "Small Bananas", "Tesco Small Bananas" ],
            "AverageSellingUnitWeight": 0.965,
            "UnitQuantity": "EACH",
            "id": 285157326,
            "ContentsQuantity": 6,
            "department": "Fresh Fruit",
            "price": 0.9,
            "unitprice": 0.15
          }
        ],
        "suggestions": []
      }
    }
  }
}

如果你看上面的json结果,你需要一个更全面的模型。

为此,您可以在此处将 json 结果输入到模型中。 look here

或使用动态保持结果对象的当前状态。

   string body = response.Content.ReadAsStringAsync().Result;

            dynamic content = JObject.Parse(body);

            var products = content.uk.ghs.products.results;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多