具有模型类型错误的MVC IPagedList

时间:2016-04-01 16:31:16

标签: asp.net-mvc-4

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

CS1061:' IPagedList'不包含' TargetContact'的定义并没有扩展方法' TargetContact'接受类型' IPagedList'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

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

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

2 个答案:

答案 0 :(得分:2)

视图中的模型是IPagedList<OutreachSetListViewModel>,因此当您在模型中循环时,每个项目都有TargetContact

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

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

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

返回的值做任何事情

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

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

答案 1 :(得分:0)

我知道我已经迟到了,但是我今天正在逐步解决这个问题并通过使用与IEnumerable相同的方法来解决所有我用IPagedList替换IEnumerable并且它就像一个魅力。

public static string DisplayNameFor<TModelItem, TResult>(this IHtmlHelper<IEnumerable<TModelItem>> htmlHelper, Expression<Func<TModelItem, TResult>> expression)
    {
        if (htmlHelper == null)
            throw new ArgumentNullException(nameof(htmlHelper));
        if (expression == null)
            throw new ArgumentNullException(nameof(expression));
        return htmlHelper.DisplayNameForInnerType(expression);
    }

public static string DisplayNameFor<TModelItem, TResult>(this IHtmlHelper<IPagedList<TModelItem>> htmlHelper, Expression<Func<TModelItem, TResult>> expression)
    {
        if (htmlHelper == null)
            throw new ArgumentNullException(nameof(htmlHelper));
        if (expression == null)
            throw new ArgumentNullException(nameof(expression));
        return htmlHelper.DisplayNameForInnerType(expression);
    }
相关问题