There is an application on asp.net. I decided to write an interface and the class of sorting of any model inherited from it which one comes to it. And everything would normally get a sorted model, but when I try to spit out the view, problems begin. Interface:

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

Class inherited from this interface:

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

Next, I have a class called UnitOfBusiness and it creates instances of all business logic classes, including this one:

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

Controller:

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

Finally, view:

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

As a result, the view crashes:

System.Collections.Generic.List 1[System.Object]', but this dictionary requires a model item of type 'PagedList.IPagedList 1 [AutoStore.Domain.Core.Car] ' .

I tried as it should, to transfer the model in the view using

  result.ToPagedList(page, 8) 

The same result. I tried to get the benefit from the error and take it to the view in the pagedlist instead of the model, object, then another error takes off.

The object reference does not indicate an object instance.

And the same error crashes if you transfer the list of sorted cars through the ViewBag. Please tell me what you can do? Already the whole head broke.

  • What type of result do you have return View(result); and what you expect in @model @model PagedList.IPagedList<AutoStore.Domain.Core.Car> view - tym32167
  • What did you really want? You honestly say that you have a List<System.Object> when the PagedList<Car> requires PagedList<Car> - AK
  • Func<T, dynamic> predSort not necessary to push dynamic anywhere, for extreme cases, you can do without it. - tym32167
  • @ tym32167 and how? I can sort on every field and I do not know what will come there? - Andrei
  • @AK I want to display a sorted list of cars on the view in the table. - Andrei

1 answer 1

Remove dynamic

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

your uob

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

Controller

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

Threat wrote on the sneaker, compiled in the mind, it's just an example of how to get away from dynamic

  • Comments are not intended for extended discussion; conversation moved to chat . - Yuriy SPb