0

I am struggling with an issue which seems very weird.

1) I have a class named 'Tumblr':

public class Tumblr
{
    public string BlogName { get; set; }
    public dynamic PhotosCollection { get; set; }
}

2) I have a method in my controller:

 public ActionResult Index()
 {
     var model = new List<Tumblr>();
     var client = new HttpClient();
     var task = client.GetAsync("http://api.tumblr.com/v2/blog/puppygifs.tumblr.com/posts/photo?api_key=SbvxQcWuuNr1mfRvILKKTqiztMGvR8hbGPBDA9PlMidrYdd8Ik")
                    .ContinueWith((taskWithMsg) =>
                     {
                         var response = taskWithMsg.Result;
                         var jsonTask = response.Content.ReadAsAsync<JObject>();
                         jsonTask.Wait();

                         var jsonResponse = jsonTask.Result;
                         model = FillModel(jsonResponse, model)
                     });
      task.Wait();
      return View(model);
  }

  private List<Tumblr> FillModel(dynamic jsonObject, List<Tumblr> model)
  {
      var postObject = (JArray)jsonObject["response"].posts;
      var posts = postObject.Select(e => new Tumblr()
                  {
                      BlogName = e["blog_name"].ToString(),
                      PhotosCollection = e["photos"].Select(o => new
                      {
                          Photos = o["alt_sizes"].Select(s => new
                          {
                              Width = s["width"].ToString(), 
                              Height = s["width"].ToString(),
                              Url = s["width"].ToString(),
                          })
                      })
                  });
      model.AddRange(posts);
      return model;
  }

And here is my view:

@using System.Collections
@{
    ViewBag.Title = "Home Page";
}


<ol class="round">
@foreach (dynamic blog in Model)
{
    <li class="one">
        <h5>@blog.BlogName</h5>
        <span>@blog.PhotosCollection</span>
        <hr />
        <hr />
        <hr />
        @foreach (dynamic photos in blog.PhotosCollection)
        {
            foreach (var photo in photos.Photos)
            {
                <img src="@photo.Url" width="@photo.Width" height="@photo.Height"/>
            }
        }
        <hr />
    </li>
}
</ol>

When I try to run, it throws an error with:

'object' does not contain a definition for 'Photos'

I do not understand why it throws an error even though I have a property in my model.

live-love
  • 48,840
  • 22
  • 240
  • 204
Ammar Khan
  • 346
  • 1
  • 9
  • 27
  • 1
    Is there a reason you're using `dynamic`s everywhere and not using `@model List` at the top of your Razor code? Also, did you know you've referred to the property as `Phots`? – Adrian Wragg Oct 27 '13 at 22:00
  • See here http://stackoverflow.com/questions/8820356/dynamic-type-in-mvc-view – glosrob Oct 27 '13 at 22:05
  • Hi,I added the model in my view but it won't take any effect. About 'Phots' that have been written by mistake. Make it right don't even resolve the issue. I am still getting the same error. The reason for using dynamic because I am call 3rd party api tumblr. If you want to watch the json response, see here http://api.tumblr.com/v2/blog/puppygifs.tumblr.com/posts/photo?api_key=SbvxQcWuuNr1mfRvILKKTqiztMGvR8hbGPBDA9PlMidrYdd8Ik – Ammar Khan Oct 27 '13 at 22:32
  • did you try to use var instead of dynamic in your foreach? It might be the next foreach would understand what it's gonna iterate over. – Lars Anundskås Oct 27 '13 at 22:42
  • Yes, I tried'var' but no success – Ammar Khan Oct 27 '13 at 22:51

2 Answers2

1

I am sure there is a better way to do this, but if you want to keep the way it is, change your view page code like this:

@model dynamic
@{
    ViewBag.Title = "Home Page";
}
<ol class="round">
    @foreach (dynamic blog in Model)
    {
        <li class="one">
            <h5>@blog.BlogName</h5>
            <span>@blog.PhotosCollection</span>
            <hr />
            <hr />
            <hr />

            @foreach (dynamic p in blog.PhotosCollection)
            {
                var photos = p.GetType().GetProperty("Photos").GetValue(p, null);
                foreach (var photo in photos)
                {
                    var url = photo.GetType().GetProperty("Url").GetValue(photo, null);
                    var width = photo.GetType().GetProperty("Width").GetValue(photo, null);
                    var height = photo.GetType().GetProperty("Height").GetValue(photo, null);

                    <img src="@url" width="@width" height="@height"/>
                }
            }
            <hr />
        </li>
    }
</ol>
Lin
  • 15,078
  • 4
  • 47
  • 49
1

Here's an example of how to access a property from the model:

View:

@model Portal.ViewModels.LayoutViewModel
@Model.UserName

View Model:

namespace Portal.ViewModels
{
    public class LayoutViewModel
    {
        public String UserName { get; set; }
    }
}
live-love
  • 48,840
  • 22
  • 240
  • 204