gpt4 book ai didi

c# - 如何在 View 中获取 View 模型的属性?

转载 作者:行者123 更新时间:2023-11-30 20:34:25 25 4
gpt4 key购买 nike

我需要在 View 中访问 View 模型的属性,因为我需要为 razor 中的某些内容赋值,但每次我键入 Model.它没有显示让我把属性放在错误旁边。

这是我的代码:

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

<h2>@Model.SelectedArticleType</h2>

@Model.SelectedArticleTypeViewBag.Title = Model.Images;错误。

错误是:

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?)

这是我的 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);
}

View 模型看起来像:

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>

}

关于c# - 如何在 View 中获取 View 模型的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39254925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com