3

I'm having a hard time displaying the values from a list object model to the view.

I've tried to display it using this code

@model IEnumerable<MVC.Models.RootObject>

@foreach (var item in @Model)
{
    <li>@item.records</li>
} 

But it shows an error

The model item passed into the dictionary is of type 'MVC.Models.RootObject', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVC.Models.RootObject]'.

Here is my controller that I used to pass data from model to view

           var transno = "ST-100420190001";

            var client = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri("https://myurl.com/" + transno),
                Headers = {
                { HttpRequestHeader.Accept.ToString(), "application/json" },
                { HttpRequestHeader.ContentType.ToString(), "application/json"},
                { "client-id", "client_id"},
                { "client-secret","client_secret"},
                { "partner-id","partner_id"},
                { "X-Version", "1" }
            }
            };

            var response = client.SendAsync(httpRequestMessage).Result;
            RootObject obj = JsonConvert.DeserializeObject<RootObject>(await 
            response.Content.ReadAsStringAsync());

            return View(obj);

The RootObject model looks like this

public class RootObject
    {
        public List<Record> records { get; set; }
        public int totalRecords { get; set; }
    }

Then the Record model looks like this

 public class Record
    {
        public string transferId { get; set; }
        public string type { get; set; }
        public DateTime createdAt { get; set; }
        public string dateUpdated { get; set; }
        public string state { get; set; }
        public string senderTransferId { get; set; }
    }
1
  • 1
    What is the @model line in your view file? The error suggests it is IEnumerable<RootObject> and yet you are passing a single RootObject. Commented May 7, 2019 at 7:24

2 Answers 2

2

Okay let's have a look at the error message

Your view is expecting IEnumerable of RootObject, but you're just passing in a single RootObject.

The model item passed into the dictionary is of type 'MVC.Models.RootObject', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVC.Models.RootObject]'.

I'm not sure what your API is replying with, but you may need to DeserializeObject to an IEnumerable of RootObject instead of a single object. have you tried debugging these lines?

var response = client.SendAsync(httpRequestMessage).Result;
RootObject obj = JsonConvert.DeserializeObject<RootObject>(await response.Content.ReadAsStringAsync());

return View(obj);

If this is not the case and you're looking to make multiple API calls to build a list at a later date

Something like this should work, but I'd recommend following the above as it seems like more what you're looking for, judging by the razor you posted.

var response = client.SendAsync(httpRequestMessage).Result;
RootObject obj = JsonConvert.DeserializeObject<RootObject>(await 
response.Content.ReadAsStringAsync());

IEnumerable<RootObject> list = new List<RootObject>(){ obj };

return View(list);

Sign up to request clarification or add additional context in comments.

2 Comments

What my api is returning is a list of 'record' object like what in my 'record' model shows. Your second suggestion works for me but is it okay to make multiple api calls to build a list? Doesn't it slows down the page?
@ikey it will slow down the page. You'll need to call an endpoint that returns a list of record object then DeserializeObject an IEnumerable<RootObject>
2

As per my comment, you want to change the @model line and potentially flesh out the loop as follows.

@model MVC.Models.RootObject

@foreach (var item in @Model.records)
{
    <li>@item.transferId</li>
    <li>@item.type</li>
    // etc
} 

5 Comments

I have encountered this error after I used your code suggestion. 'IEnumerable<RootObject>' does not contain a definition for 'records' and no extension method 'records' accepting a first argument of type 'IEnumerable<RootObject>' could be found
@ikey That is because you have not gave records any value, therefore it is null
But after i debug the program I see that a value is passed to records. Maybe this code is the problem? IEnumerable<MVC.Models.RootObject>
@ikey This does not sound like you have changed the foreach correctly. Have you editted it to loop through the records instead of the model? / changed the view model from a list to a single model
I've added the @model line in my answer, it should just be the RootObject not a collection of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.