传递模型对象和列表以查看

时间:2017-04-05 12:50:30

标签: c# asp.net-mvc model

我是ASP.NET MVC的新手,所以请耐心等待。我试图将模型对象和List传递给我的viewmodel。

这是我的代码:

ViewModel:
    public class SchemaModel
    {
        public IEnumerable<AS_AVTAL_SCHEMA> Schema { get; set; }
        public IEnumerable<AS_AVTAL_SCHEMA> Ar { get; set; }
        public IEnumerable<AS_AVTAL_SCHEMA> Veckor { get; set; }

    }


Controller:
 public ActionResult Schema(int schemaID)
        {
            using (var db = new TTSPEntities())
            {
                var veckor = (from AS_AVTAL_SCHEMA in db.AS_AVTAL_SCHEMA
                              where AS_AVTAL_SCHEMA.AS_AH_LNR == (schemaID)
                              select new
                              {
                                  AS_AVTAL_SCHEMA.AS_SC_VECKA
                              }).ToString().Distinct().ToList();

                var model = new SchemaModel
                {
                    Schema = db.AS_AVTAL_SCHEMA.Where(A => A.AS_AH_LNR == schemaID).ToList(),
                };
                return View(model, veckor);
            }
        }

我收到以下错误: 参数2:无法从'System.Collections.Generic.list'转换为'string'。

我一直试图通过其他几种方式来解决这个问题,但似乎无法做到这一点。我想我的数据类型和强制转换有问题。

有人有任何想法吗?

编辑: 如果我这样做,这对我来说更有意义,我仍然会收到错误。

控制器:

public ActionResult Schema(int schemaID)
        {
            using (var db = new TTSPEntities())
            {

                ////Hämtar veckor och sparar varje unikt veckonummer i en lista.
                var veckor = (from AS_AVTAL_SCHEMA in db.AS_AVTAL_SCHEMA
                              where AS_AVTAL_SCHEMA.AS_AH_LNR == (schemaID)
                              select new
                              {
                                  AS_AVTAL_SCHEMA.AS_SC_VECKA
                              }).Distinct().ToList();

                var model = new SchemaModel
                {
                    Schema = db.AS_AVTAL_SCHEMA.Where(A => A.AS_AH_LNR == schemaID).ToList(),
                    Veckor = veckor
                };
                return View(model);
            }
        }

我收到以下错误:

  

错误CS0266无法隐式转换类型   'System.Collections.Generic.List&LT;&GT;'   至   'System.Collections.Generic.IEnumerable'。   存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:0)

这样做可以解决我的问题:

Controller:                 
                var model = new SchemaModel
                {
                    Schema = db.AS_AVTAL_SCHEMA.Where(A => A.AS_AH_LNR == schemaID).ToList(),
                    AS_SC_VECKA = (from AS_AVTAL_SCHEMA in db.AS_AVTAL_SCHEMA
                                   where AS_AVTAL_SCHEMA.AS_AH_LNR == (schemaID)
                                   select AS_AVTAL_SCHEMA.AS_SC_VECKA
                              ).Distinct().ToList()
            };

Viewmodel:
    public class SchemaModel
    {
        public List<int?> AS_SC_VECKA { get; set; }
    }