简体   繁体   English

如何获取视图中视图模型的属性?

[英]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.SelectedArticleTypeViewBag.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?) 严重性代码说明项目文件行抑制状态错误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.

相关问题 如何将相同的视图模型用于不同的视图(排除必需的属性)? - How do I use same view model but for different view excluding the required properties? 如何在视图模型中获取鼠标位置 - How do I get mouse positions in my view model 如何将模型验证装饰输出到我的视图? - How do I get the model validation decorations output to my view? 如何在PropertyGrid中查看对象属性? - How do I view object properties in PropertyGrid? ASP.NET MVC:如何处理具有许多属性的视图模型? - ASP.NET MVC: How do I handle a view model with many properties? 当父视图具有相同模型时,如何将局部视图中的 HTML 表单数据序列化? - How do I get HTML form data in Partial View to be serialized when parent view has same model? 如何使用foreach将模型中的属性输入到视图中? - How can I get my properties from a Model into my View with a foreach? 如何在运行时设置视图/视图模型数据模板? - How do I set view/view model data template at runtime? 模型属性的属性通过视图模型查看 - 如何? - Attributes of model properties goes to view through view model - how to? 如何将模型属性传递给具有其他模型的局部视图? - How do I pass a model property to a partial view with a different model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM