[英]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. 我需要在视图内部访问视图模型的属性,因为我需要为razor中的某物分配一个值,但是每次我键入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;
@Model.SelectedArticleType
和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 typeIPagedList<ArticlesViewModel>
could be found (are you missing a using directive or an assembly reference?)严重性代码说明项目文件行抑制状态错误CS1061
IPagedList<ArticlesViewModel>
不包含'Images'的定义,并且找不到扩展方法'Images'接受类型为IPagedList<ArticlesViewModel>
的第一个参数(是否缺少using指令或装配参考?)
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 : Viewmodel看起来像:
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 引用PagedList对象而不是接口
@model PagedList<New_MinecraftNews_Webiste_MVC.Models.ArticlesViewModel>
@using PagedList.Mvc;
@using PagedList;
PagedList is a list. PagedList是一个列表。 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. 如果您需要页面上的某些“标题”类型信息以及与该标题信息相关联的列表,则可能需要为该列表创建局部视图,并通过标题信息类的属性将列表传递进来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.