Kyle BC Kyle BC - 1 year ago 77
C# Question

How do I get to the properties of the view model in the view?

I need to access the properties of the view model inside the view because I need to assign a value to something in razor but every time I type Model. It doesn't show let me put the properties next it errors.

Here is my code:

@model PagedList.IPagedList<New_MinecraftNews_Webiste_MVC.Models.ArticlesViewModel>
@using PagedList.Mvc;
ViewBag.Title = Model.Images;


ViewBag.Title = Model.Images;

The error is:

Severity Code Description Project File Line Suppression State
Error CS1061
does not contain a
definition for 'Images' and no extension method 'Images' accepting a
first argument of type
could be found
(are you missing a using directive or an assembly reference?)

Here is my controller:

public ActionResult ArticleTypes(string at, int page = 1, int pageSize = 15)

articleViewModel.Images = new List<ImageInfo>();
var modelList = (from a in db.Articles
where a.SelectedArticleType == at
orderby a.Id descending
select new ArticlesViewModel
Id = a.Id,
Body = a.Body,
Headline = a.Headline,
PostedDate = a.PostedDate,
SelectedArticleType = a.SelectedArticleType,
UserName = a.UserName

foreach (var item in modelList)
item.Images = imageService.GetImagesForArticle(item.Id);

PagedList<ArticlesViewModel> model = new PagedList<ArticlesViewModel>(modelList, page, pageSize);

return View(model);

Viewmodel looks like :

public class ArticlesViewModel
public int Id { get; set; }

public List<SelectListItem> ArticleType { get; set; }

public string SelectedArticleType { get; set; }

public string UserName { get; set; }
public string Headline { get; set; }
public string Body { get; set; }
public DateTime PostedDate { get; set; }

public ImageInfo MainImage { get; set; }

public List<ImageInfo> Images { get; set; }

Answer Source
@model PagedList.IPagedList<New_MinecraftNews_Webiste_MVC.Models.ArticlesViewModel>
@using PagedList.Mvc;
    ViewBag.Title = "Page title goes here";

@foreach (var article in Model) {


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download