带有模型类型错误的MVC IPagedList [英] MVC IPagedList with model type error

查看:75
本文介绍了带有模型类型错误的MVC IPagedList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 5应用中收到错误:

I get the error in my MVC 5 App:

CS1061:"IPagedList"不包含"TargetContact"的定义,找不到可以接受类型为"IPagedList"的第一个参数的扩展方法"TargetContact"(您是否缺少using指令或程序集引用?)

CS1061: 'IPagedList' does not contain a definition for 'TargetContact' and no extension method 'TargetContact' accepting a first argument of type 'IPagedList' could be found (are you missing a using directive or an assembly reference?)

我在这里看到了答案,但仍然没有完成:( 这可能很容易解决.

I saw the answers here but I still don’t get it done :( It's probably pretty easy to solve.

public ActionResult Index(string searchTargetContact = null, int page = 1)
        {
                var model =
                from r in db.Outreach
                orderby r.TargetContact descending
                where (r.TargetContact.StartsWith(searchTargetContact) || searchTargetContact == null)
                select new Models.OutreachSetListViewModel
                {
                    TargetContact = r.TargetContact,
                    NextOutreachStep = r.NextOutreachStep,
                    GoalOfOutreach = r.GoalOfOutreach,
                                    };
            model.ToPagedList(page, 10);

 return View(model);

namespace WebApplication11.Models
{
    public class OutreachSetListViewModel
    {
        public string NextOutreachStep { get; set; }
        public string TargetContact { get; set; }
        public string GoalOfOutreach { get; set; }
   }
}

@model IPagedList<OutreachSetListViewModel>
<table class="table" id="networkingList">
    <tr>

        <th>@Html.DisplayNameFor(model => model.TargetContact)</th>
        <th>@Html.DisplayNameFor(model => model.NextOutreachStep)</th>
        <th>@Html.DisplayNameFor(model => model.GoalOfOutreach)</th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.TargetContact)</td>
            <td>@Html.DisplayFor(modelItem => item.NextOutreachStep)</td>
            <td>@Html.DisplayFor(modelItem => item.GoalOfOutreach)</td>
</tr>
}

推荐答案

视图中的模型为IPagedList<OutreachSetListViewModel>,因此,当您遍历模型时,每个项目的确有一个TargetContact.

The model in the view is IPagedList<OutreachSetListViewModel>, so when you are looping though the model, each item does have a TargetContact.

但是,当显示标题时,DisplayNameFor的模型不是单个项目,而是列表.该列表没有TargetContact属性,因此我们必须从列表中的一项获得它.

However, when you are displaying the header, the model for the DisplayNameFor is not the individual item, but the list. The list does not have the TargetContact property so we have to get it from one of the items in the list.

在这种情况下,我们检查列表中是否有任何元素,如果有,请从第一个元素获取TargetContact.

In this case, we check to see if there are any elements in the list, and if there are, get the TargetContact from the first element.

@if(Model.Any())
{
    <tr>

        <th>@Html.DisplayNameFor(model => model[0].TargetContact)</th>
        <th>@Html.DisplayNameFor(model => model[0].NextOutreachStep)</th>
        <th>@Html.DisplayNameFor(model => model[0].GoalOfOutreach)</th>
        <th></th>
    </tr>
}

控制器

对于从model.ToPagedList(page, 10);

将其保存为一个值并将其传递给视图:

Save it to a value and pass it in to the view:

var vm = model.ToPagedList(page, 10);
return View(vm);

这篇关于带有模型类型错误的MVC IPagedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆