Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3683

Uable to create view from models, created from complex json classes

$
0
0

This is my string in index method, I am trying to convert my JSON string to object of MultipleClasses and want to display in my view but unable to do that.

if (response.IsSuccessStatusCode){    data = response.Content.ReadAsStringAsync().Result;    MultipleClasses GetData = JsonConvert.DeserializeObject<MultipleClasses>(data);    return View(GetData );}

This is ViewModel Class

using ExternalAPIModule.Models;using Microsoft.AspNetCore.Mvc.Rendering;namespace ExternalAPIModule.ViewModel{    public class MultipleClasses    {        public User? User;        public List<Users>? products { get; set; }        public List<variant>? variants;    }}

This is my Model class (Copying as it is)

using Newtonsoft.Json;//using static ExternalAPIModule.Models.User.Users;using static ExternalAPIModule.Models.User;namespace ExternalAPIModule.Models{    public class User    {        public Users[] products { get; set; }    }    public class Users    {        [JsonProperty("id")]        public double? id { get; set; }        [JsonProperty("vendor")]        public string? vendor { get; set; }        [JsonProperty("title")]        public string? title { get; set; }        [JsonProperty("created_at")]        public string? created_at { get; set; }        public string? body_html { get; set; }        public variant[] variants { get; set; }    }    public class variant    {        public string? price { get; set; }        public string? compare_at_price { get; set; }    }}

How can I display the model in View to show JSON data?? I want to convert the JSon to my view using ViewModel in ASP.NET MVC

Currently I am trying to use this view that is giving error:

@model IEnumerable<ExternalAPIModule.ViewModel.MultipleClasses><table class="table"><thead><tr><th>                @Html.DisplayNameFor(model => model.products.id)</th><th>                @Html.DisplayNameFor(model => model.products.vendor)</th><th>                @Html.DisplayNameFor(model => model.products.title)</th><th>                @Html.DisplayNameFor(model => model.products.created_at)</th><th>                @Html.DisplayNameFor(model => model.products.body_html)</th><th>                @Html.DisplayNameFor(model => model.variant.price)</th><th>                @Html.DisplayNameFor(model => model.variant.compare_at_price)</th><th></th></tr></thead><tbody>        @foreach (var item in Model)        {<tr><td>                    @Html.DisplayFor(modelItem => item.products.id)</td><td>                    @Html.DisplayFor(modelItem => item.products.vendor)</td><td>                    @Html.DisplayFor(modelItem => item.products.title)</td><td>                    @Html.DisplayFor(modelItem => item.products.created_at)</td><td>                    @Html.DisplayFor(modelItem => item.products.body_html)</td><td>                    @Html.DisplayFor(modelItem => item.variant.price)</td><td>                    @Html.DisplayFor(modelItem => item.variant.compare_at_price)</td><td>                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })</td></tr>        }</tbody></table>

Viewing all articles
Browse latest Browse all 3683

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>