【问题标题】:MVC IPagedList with model type error模型类型错误的 MVC IPagedList
【发布时间】:2016-04-01 16:31:15
【问题描述】:

我的 MVC 5 应用程序出现错误:

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

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

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

【问题讨论】:

    标签: asp.net-mvc-4


    【解决方案1】:

    视图中的模型是IPagedList&lt;OutreachSetListViewModel&gt;,因此当您循环遍历模型时,每个项目都有一个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);
    

    【讨论】:

    • 谢谢!现在它可以识别属性,但我收到以下错误 The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery'1[WebApplication11.Models.OutreachSetListViewModel]', but this dictionary requires a model item of type 'PagedList.IPagedList1[WebApplication11.Models.OutreachSetListViewModel]'。`
    • 更新了我的回复。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多