这个模型错误在asp.net mvc中说了什么?

时间:2010-05-01 09:55:20

标签: asp.net-mvc model

我的Html操作链接将我带到一个视图,我可以看到详细信息.. 对于Ex:http://localhost:1985/Materials/Details/4 但它显示错误,

The model item passed into the dictionary is of type 
'System.Data.Linq.DataQuery`1[CrMVC.Models.ConstructionRepository+Materials]' 
but this dictionary requires a model item of type 
'CrMVC.Models.ConstructionRepository+Materials'.

我的模特是......

         public IQueryable<Materials> GetMaterial(int id)
        {
            return from m in db.Materials
                            join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
                            where m.Mat_id == id
                            select new Materials()
                            {
                                Id = Convert.ToInt64(m.Mat_id),
                                Mat_Name = m.Mat_Name,
                                Mes_Name = Mt.Name,
                            };
        }
        public class Materials
        {
            private Int64 id;
            public string mat_Name;
            public string mes_Name;
            public Int64 Id
            {
                get
                {
                    return id;
                }
                set
                {
                    id = value;
                }
            }
            public string Mat_Name
            {
                get
                {
                    return mat_Name;
                }
                set
                {
                    mat_Name = value;
                }
            }
            public string Mes_Name
            {
                get
                {
                    return mes_Name;
                }
                set
                {
                    mes_Name = value;
                }
            }
        }
    }

和我的控制器方法......

public ActionResult Details(int id)
{
    var material = consRepository.GetMaterial(id).AsQueryable();
    return View("Details", material);
}

有什么建议我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

您的GetMaterial(id)方法应该只返回一个Material实例,因为您的View只需要一个实例。 E.g:

public Materials GetMaterial(int id)
{
    return (from m in db.Materials
            join Mt in db.MeasurementTypes on m.MeasurementTypeId equals Mt.Id
            where m.Mat_id == id
            select new Materials()
            {
                Id = Convert.ToInt64(m.Mat_id),
                Mat_Name = m.Mat_Name,
                Mes_Name = Mt.Name,
            }).FirstOrDefault();
    }