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'

1
  • 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. Commented Mar 4, 2014 at 9:44

1 Answer 1

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

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

Comments

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.