3

Hello friend I have a problem in mvc 4 to send a dynamic model in View suggest me how to bind dynamic model with the view

Here is my Action

[ChildActionOnly]

    public ActionResult TopArticles()
    {
        var model = from a in db.Articles.Take(3).OrderBy(m=>m.TotalViews) 
                    join u in db.UserProfiles 
                        on a.CreatedBy equals u.ID 
                    join t in db.Tags 
                        on a.TagID equals t.ID 
                    select new 
                    { 
                        Title = a.Title,
                       ArticleID = a.ArticleID, 
                        FirstName = u.FirstName, 
                        CreatedBy = a.CreatedBy, 
                        TagID = a.TagID, 
                        TagName = t.Name, 
                        CreationDate = a.CreationDate, 
                        TotalViews = a.TotalViews 
                    };  // get Top 3 Articles from Articles
        return PartialView(model);
    }

And Here is my View

 @model IEnumerable<dynamic>
@{
    ViewBag.Title = "TopArticles";   
}
@foreach(dynamic item in Model)
{
   <div class="row about_topviewedarticle1">
   <div class="row">
     <div class="columns twenty">
     <a href="#" class="title">@item.Title</a>
by   <a href="#"class="profilefont_eleven">@item.FirstName</a>
                    </div>
                </div>
                <div class="row">
                    <div class="columns twenty">
                        <label class="font_ten">views: @item.TotalViews  posted on : @item.CreationDate</label>
                    </div>
                </div>
            </div>
            }

When I run this code following error is showing

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

tereško
  • 58,060
  • 25
  • 98
  • 150
  • You have to make a view model, e.g. ArticlesViewModel, which will contains this parts of Articles entity which you want to show. After that in the query you will have ...select new ArticlesViewModel { ... }. Finally pass the view model to the view. – vladislavn Mar 04 '14 at 09:44

1 Answers1

2

1st way

try this

var model = from a in db.Articles.Take(3).OrderBy(m=>m.TotalViews) 
                    join u in db.UserProfiles 
                        on a.CreatedBy equals u.ID 
                    join t in db.Tags 
                        on a.TagID equals t.ID 
                    **select new Article**
                    { 
                        Title = a.Title,
                       ArticleID = a.ArticleID, 
                        FirstName = u.FirstName, 
                        CreatedBy = a.CreatedBy, 
                        TagID = a.TagID, 
                        TagName = t.Name, 
                        CreationDate = a.CreationDate, 
                        TotalViews = a.TotalViews 
                    };

According to David Ebbo, you can't pass an anonymous type into a dynamically-typed view because the anonymous types are compiled as internal. Since the CSHTML view is compiled into a separate assembly, it can't access the anonymous type's properties.

See this discussion

or,

2nd Way

converting the anonymous object into an ExpandoObject right away.

public static ExpandoObject ToExpando(this object anonymousObject)
{
    IDictionary<string, object> anonymousDictionary =  new RouteValueDictionary(anonymousObject);
    IDictionary<string, object> expando = new ExpandoObject();
    foreach (var item in anonymousDictionary)
        expando.Add(item);
    return (ExpandoObject)expando;
}



 var model = from a in db.Articles.Take(3).OrderBy(m=>m.TotalViews) 
                        join u in db.UserProfiles 
                            on a.CreatedBy equals u.ID 
                        join t in db.Tags 
                            on a.TagID equals t.ID 
                      select new 
                        { 
                            Title = a.Title,
                           ArticleID = a.ArticleID, 
                            FirstName = u.FirstName, 
                            CreatedBy = a.CreatedBy, 
                            TagID = a.TagID, 
                            TagName = t.Name, 
                            CreationDate = a.CreationDate, 
                            TotalViews = a.TotalViews 
                        }**.ToExpando()**;

referred from here and here

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234