简体   繁体   中英

How do I return data in my MVC application based on Ids passed in using an array to a Kendo Grid?

I want to pass an array of ids to my controller and return only the records with matching ids to my Kendo Grid.

Here is what I have so far, my javascript function and a basic array.

var cars = [1,2,3];
function myParams() {
    return {
        array: cars
    }
}

Here is my controller.

private UnitOfWork unitOfWork = new UnitOfWork();

public ActionResult GetTabCars(int[] array, [DataSourceRequest] DataSourceRequest request)
{
    var car = unitOfWork.CarRepository.Get().Where(u => array.Any(x => x == u.Id));
        var result = car.ToDataSourceResult(request);
        return Json(result, JsonRequestBehavior.AllowGet);        
}

My unit of work is a generic repository. Here is the Get metod from GenericRepository.cs

public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            string includeProperties = "")
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

            foreach (var includeProperty in includeProperties.Split
                (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProperty);
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

Because the Ids are of type int I can't use .Contains . Although my controller method doesn't report any errors, there is no data in my grid. Have I done something wrong?

Issue with your code is

var car = unitOfWork.CarRepository.Get().Where(u => array.Any(x => x == u.Id));

You are querying a list( unitOfWork.CarRepository.Get() ) which will always result in count 0 as arguments haven't been passed),so it won't ever give you result.

I am also not sure what expression should be passed but there is a workaround. Probably someone will provide you a better answer but till den you might use it.

In your repository there must be a method to fetch single record say FindSingleOrDefault() . If FindSingleOrDefault() isn't there you can paste this code

public TEntity FindSingleOrDefault(Expression<Func<TEntity, bool>> predicate)
        {
            return _context.Set<TEntity>().SingleOrDefault(predicate);
        }

so you can use this.

List<Car> cars=new List<Car>();
foreach(var id in array)
{
  cars.Add(unitOfWork.CarRepository.FindSingleOrDefault(x=>x.id==id));
}

Cars will contain all the car records with matching id.

If it was not repository pattern then simple linq query would have been like

var cars = (from id in array
            join car in dbSet.Cars
            on id equals car.id
            select car).ToList();

Let me know if you face issue applying this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM