不能将字符串转换为Model类型

时间:2016-11-20 19:22:28

标签: c# asp.net-mvc entity-framework

我有一个ViewModel

  public class SubjectOverviewViewModel
    {
        [DisplayName("Neptunkód")]
        public Subject NeptunId { get; set; }
        public SubjectContent TaFoAdatok { get; set; }
        public SubjectContent TaAdatok { get; set; }
        public SubjectContent TaOktatok { get; set; }
        public SubjectContent TaKurzusok { get; set; }
        public SubjectContent IrodalomLista { get; set; }
        public virtual SubjectContent SubjectContent { get; set; }

        public virtual Subject Subject { get; set; }
    }

和控制器:

irfwebpage20161013070934_dbEntities2 db = new irfwebpage20161013070934_dbEntities2(); //dbcontext class
            List<SubjectOverviewViewModel> list = new List<SubjectOverviewViewModel>(); // to hold list of Customer and order details
            var customerlist = (from x in db.Subject
                                join c in db.SubjectContent on x.NeptunId equals c.NeptunId
                                select new SubjectOverviewViewModel { NeptunId=c.NeptunId,
                                    TaAdatok=c.TaAdatok,
                                    TaFoAdatok=c.TaFoAdatok,
                                    IrodalomLista=c.IrodalomLista,
                                    TaKurzusok=c.TaKurzusok });

select new SubjectOverviewViewModel部分,我收到错误,例如c.TaAdatok是字符串,它不能隐式地将其转换为模型类型。第一个争论的NeptunID没有显示任何错误,尽管它也像其他类似的字符串类型。请帮助我被困在这里。我查看了StackOverflow上的其他线程,但是我已经实现并且没有工作的那些解决方案。

public partial class SubjectContent
    {
        public string NeptunId { get; set; }
        public string TaFoAdatok { get; set; }
        public string TaAdatok { get; set; }
        public string TaOktatok { get; set; }
        public string TaKurzusok { get; set; }
        public string IrodalomLista { get; set; }

        public virtual Subject Subject { get; set; }
    }

 public partial class Subject
    {
        public string NeptunId { get; set; }
        public string Name { get; set; }

        public virtual Node Node { get; set; }
        public virtual SubjectContent SubjectContent { get; set; }
        public virtual SubjectRating SubjectRating { get; set; }

        public static implicit operator Subject(string v)
        {
            throw new NotImplementedException();
        }
    }

Subject和SubjectContent由实体数据建模的数据库优先方法创建。

1 个答案:

答案 0 :(得分:0)

问题在于:

var customerlist = (from x in db.Subject
                            join c in db.SubjectContent on x.NeptunId equals c.NeptunId
                            select new SubjectOverviewViewModel { NeptunId=c.NeptunId,
                                TaAdatok=c.TaAdatok,
                                TaFoAdatok=c.TaFoAdatok,
                                IrodalomLista=c.IrodalomLista,
                                TaKurzusok=c.TaKurzusok });

c.TaAdatokstringSubjectOverviewViewModel.TaAdatok的类型为SubjectContent

我认为视图模型中的属性类型是错误的。试试这个:

public class SubjectOverviewViewModel
{
    [DisplayName("Neptunkód")]
    public string NeptunId { get; set; }
    public string TaFoAdatok { get; set; }
    public string TaAdatok { get; set; }
    public string TaOktatok { get; set; }
    public string TaKurzusok { get; set; }
    public string IrodalomLista { get; set; }
}

您还可以大大简化您的查询,因为正如@GertArnold在评论中所说,您已在SubjectContent上拥有Subject导航属性:

var customerlist = from s in db.Subject
                   let sc = s.SubjectContent
                   select new SubjectOverviewViewModel
                   {
                       NeptunId = sc.NeptunId,
                       TaAdatok = sc.TaAdatok,
                       TaFoAdatok = sc.TaFoAdatok,
                       IrodalomLista = sc.IrodalomLista,
                       TaKurzusok = sc.TaKurzusok
                   };

作为最后一句话,为什么要调用变量customerlist,即使它包含有关主题的数据?