简体   繁体   中英

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; }
}
@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>

}

Reference the PagedList object and not the interface

@model PagedList<New_MinecraftNews_Webiste_MVC.Models.ArticlesViewModel>
@using PagedList.Mvc;
@using PagedList;

PagedList is a list. So you have to access the child items

 @for (int i = 0; i < Model.Count(); i++)
 {
     @Html.DisplayFor(modelItem => Model[i].SelectedArticleType)

 }

If you need some 'header' type info on the page and a list associated with that header info, you will probably need to create a partial view for the list and pass the list in via a property on the header info class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM