Kyle BC Kyle BC - 3 months ago 9
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;
}

<h2>@Model.SelectedArticleType</h2>


the
@Model.SelectedArticleType
and
ViewBag.Title = Model.Images;
errors.

The error is:


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


Here is my controller:

[Route("Articles/{at}")]
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
}).ToList();


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; }
[Required]
public string Headline { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Body { get; set; }
[DataType(DataType.Date)]
public DateTime PostedDate { get; set; }

public ImageInfo MainImage { get; set; }

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

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

@foreach (var article in Model) {

    <h1>@article.Headline</h1>
    <h2>@article.SelectedArticleType</h2>

}