'&System.Collections.Generic.List LT;>'在路线属性

时间:2016-11-16 13:03:39

标签: c# asp.net-mvc generics

我在餐厅控制器中有两个类似下面的动作

[HttpPost]
public ActionResult searchOpenRestaurants(FormCollection form)
{
    TimeSpan currentTime = DateTime.Now.TimeOfDay;
    var openRestaurants = (from t in db.Restaurants
        where
            t.OpeningTime == currentTime ||
            t.OpeningTime < currentTime ||
            t.ClosingTime > currentTime ||
            t.ClosingTime == currentTime
        select new RestaurantViewModel()
        {
            RestId = t.Id,
            RestaurantName = t.RestaurantName,
            Logo = t.Logo,
            Address = t.Address,
            City = t.City,
            District = t.District,
            TimetakentoDeliver = t.TimetakentoDeliver,
            categories = t.Restaurant_Type.Select(a => a.Category.category1).ToList(),
        }).ToList();

    return View("Index", openRestaurants.ToList());
}

[Route("Index/{restaurants:System.Collections.Generic.List<RestaurantViewModel>}")]
public ActionResult Index(List<RestaurantViewModel> restaurants)
{
    return View(restaurants);
}

我还有另外两个索引操作,如下面的

public ActionResult Index()
        {
            using (TheFoodyContext db = new TheFoodyContext())
            {

                var model = (from p in db.Restaurants // .Includes("Addresses") here?
                             select new RestaurantViewModel()
                             {
                                 RestId = p.Id,
                                 RestaurantName = p.RestaurantName,
                                 Logo = p.Logo,
                                 Address = p.Address,
                                 City = p.City,
                                 District = p.District,
                                 TimetakentoDeliver = p.TimetakentoDeliver,
                                 categories = p.Restaurant_Type.Select(a => a.Category.category1).ToList(),
                             });

                return View(model.ToList());
                //return View(db.Restaurants.ToList());
            }

        }

        [HttpPost]
        [Route("Index/{search:string}")] 
        // GET: Restaurant
        public ActionResult Index(string search)
        {
            using (TheFoodyContext db = new TheFoodyContext())
            {

                var model = (from p in db.Restaurants // .Includes("Addresses") here?
                             where p.City.StartsWith(search) || search == null
                             select new RestaurantViewModel()
                             {
                                 RestId = p.Id,
                                 RestaurantName = p.RestaurantName,
                                 Logo = p.Logo,
                                 Address = p.Address,
                                 City = p.City,
                                 District = p.District,
                                 TimetakentoDeliver = p.TimetakentoDeliver,
                                 categories = p.Restaurant_Type.Select(a => a.Category.category1).ToList(),
                             });

                return View(model.ToList());
                //return View(db.Restaurants.ToList());
            }

但这给我一个错误:

  

类型&#39; DefaultInlineConstraintResolver&#39;的内联约束解析器无法解决以下内联约束:&#39; System.Collections.Generic.List&lt; RestaurantViewModel&gt;&#39;。

任何人都可以建议我实现这个问题的方法吗?

1 个答案:

答案 0 :(得分:1)

问题在于你的路线:

[Route("Index/{restaurants}")] 

尝试:

@OneToOne
@JoinColumn(name = "START_ID")
private OneEntity e1;

@OneToOne
@JoinColumn(name = "END_ID")
private OneEntity e2;

@ElementCollection
private List<OneEntity > set = new ArrayList<>();

这里有一个DefaultInlineConstraintResolver列表: DefaultInlineConstraintResolver Error in WebAPI 2