chris
chris

Reputation: 561

MVC Viewmodel cant access model with View

How can I access my viewmodel from my view? my code is as follows:-,

I have two models (using entity framework) which have a view model of:-

 public class ViewModelStory
{
    public IEnumerable<tbl_GCB_NewsItem> GCB_NewsItem { get; set; }
    public IEnumerable<tbl_GCB_ItemComment> comemnts { get; set; }

}

My contoller populates the models by:-

        ViewModelStory.GCB_NewsItem = (from i in db.tbl_GCB_NewsItem
                    where i.intItemIdentifier.ToString() == StoryId
                    select i).SingleOrDefault();

        ViewModelStory.comemnts = (from i in db.tbl_GCB_ItemComment
                                       where i.intItemIdentifier.ToString() == StoryId
                                       select i).ToList<tbl_GCB_ItemComment>();

I return the model by

      return PartialView("NewsStory", ViewModelStory);

then in my view I have the following declaration

@model ViewModelStory
@using GCBSMVC.Models

To access my model I have tried various from Linq to and directly querying the model, but nothing seems to work:- Html.DisplayFor(m =>m.GCB_NewsItem. .... ViewModelStory.GCB_NewsItem.strItemCategory Html.Raw(System.Web.HttpUtility.HtmlDecode(ViewModelStory.GCB_NewsItem.strItemHeadline))

Upvotes: 0

Views: 1678

Answers (1)

Artur Kedzior
Artur Kedzior

Reputation: 4263

You are passing the type of you model class instead of the actual class. Try this:

var model = new ViewModelStory();
model.GCB_NewsItem = (from i in db.tbl_GCB_NewsItem
                    where i.intItemIdentifier.ToString() == StoryId
                    select i).SingleOrDefault();

model.comemnts = (from i in db.tbl_GCB_ItemComment
                                       where i.intItemIdentifier.ToString() == StoryId
                                       select i).ToList<tbl_GCB_ItemComment>();

return PartialView("NewsStory", model);

Upvotes: 1

Related Questions