RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 932865
Accepted
Андрей
Андрей
Asked:2020-01-16 21:27:41 +0000 UTC2020-01-16 21:27:41 +0000 UTC 2020-01-16 21:27:41 +0000 UTC

排序模型时出现类型转换错误

  • 772

在 asp.net 上有一个应用程序。我决定为任何涉及它的模型编写一个接口和一个从它继承的排序类。一切通常都会得到一个排序的模型,但是当我试图在视图中吐出它时,问题就开始了。界面:

public interface IModelSort<T> where T : class
{
    IEnumerable<T> GetModelSort(IEnumerable<T> model, Func<T, dynamic> predSort, string typeSort);
}

继承自该接口的类:

public class ModelSort<T> : IModelSort<T> where T : class
{
    public IEnumerable<T> GetModelSort(IEnumerable<T> model, Func<T, dynamic> predSort, string typeSort = "Asc")
    {
        if (typeSort == "Asc")
            return model.OrderBy(predSort);
        else
            return model.OrderByDescending(predSort);
    }
}

接下来,我有一个名为 UnitOfBusiness 的类,它创建所有业务逻辑类的实例,包括这个:

public class UnitOfBusiness
{
    private ModelSort<dynamic> modelSorting;

    public ModelSort<dynamic> ModelSorting
    {
        get
        {
            if (modelSorting == null)
                modelSorting = new ModelSort<dynamic>();
            return modelSorting;
        }
    }
}

控制器:

public ActionResult Index(string sort, int page = 1)
{
    IEnumerable<Car> cars = unitOfWOrk.Cars.GetAll();
    var result = unitOfBusiness.ModelSorting.GetModelSort(cars, i => i.Price).ToList();
    return View(result);
}

最后查看:

@model PagedList.IPagedList<AutoStore.Domain.Core.Car>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

<h1>Каталог автомобилей</h1>

<style>
    html,
    body {
        height: 100%;
    }

    body {
        display: -ms-flexbox;
        display: flex;
        -ms-flex-align: center;
        padding-top: 40px;
        padding-bottom: 40px;
    }
</style>

<table class="table table-bordered">
    <thead>
        <tr>
            <th>
                @Html.ActionLink("Марка", "Index")
            </th>
            <th>Модель</th>
            <th>
                @Html.ActionLink("Цена", "Index")
            </th>
            <th>Количество на складе</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in ViewBag.Cars as List<AutoStore.Domain.Core.Car>)
        {
            <tr>
                <td>@Html.DisplayFor(i => item.Mark.MarkName)</td>
                <td>@Html.DisplayFor(i => item.CarModel.ModelName)</td>
                <td>@Html.DisplayFor(i => item.Price)</td>
                <td>@Html.DisplayFor(i => item.Count)</td>
            </tr>
        }
    </tbody>
</table>

结果,视图出现错误:

传入字典的模型项的类型为“System.Collections.Generic.List 1[System.Object]', but this dictionary requires a model item of type 'PagedList.IPagedList1[AutoStore.Domain.Core.Car]”。

我尝试使用

 result.ToPagedList(page, 8)

结果相同。我试图从错误中获益,并将其放在页面列表中的视图上,而不是模型、对象上,然后另一个错误发生了。

对象引用不指向对象的实例。

如果您通过 ViewBag 传递已排序的机器列表,则会发生同样的错误。请告诉我可以做什么?我已经伤透了脑筋。

c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    tym32167
    2020-01-16T21:52:14Z2020-01-16T21:52:14Z

    删除动态

    public interface IModelSort<T> where T : class
    {
        IEnumerable<T> GetModelSort<K>(IEnumerable<T> model, Func<T,K> sortFunction, bool ask);
    }
    
    public class ModelSort<T> : IModelSort<T> where T : class
    {
        public IEnumerable<T> GetModelSort<K>(IEnumerable<T> model, Func<T,K> acessor, bool ask)
        {
            return ask ? model.OrderBy(acessor) : model.OrderByDescending(acessor);
        }
    }
    

    你的大华银行

    public class UnitOfBusiness<T> where T : class
    {
        private ModelSort<T> modelSorting;
    
        public ModelSort<T> ModelSorting
        {
            get
            {   
                if (modelSorting == null)
                    modelSorting = new ModelSort<T>();
                return modelSorting;
            }
        }
    }
    

    控制器

    public ActionResult Index(string sort, int page = 1) 
    { 
        IEnumerable<Car> cars = unitOfWOrk.Cars.GetAll(); 
        var result = unitOfBusiness.ModelSorting.GetModelSort(cars, c=>c.Price, true).ToList(); 
        return View(result.ToPagedList(page, 8)); 
    } 
    

    PS我写在拖鞋上,整理在脑海里,这只是一个例子,说明如何摆脱动态

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5