views:

58

answers:

1

My Html action link takes me to a view where i can see the details.. For Ex:http://localhost:1985/Materials/Details/4 But it shows error,

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'.

And my model is...

         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;
                }
            }
        }
    }

and my controller method...

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

Any suggestion what am i missing here?

+1  A: 

Your GetMaterial(id) method should probably return just one Material instance because your View expects just one instance. 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();
    }
M4N
@Martin just changed The return type... It worked....
Pandiya Chendur
@Martin my repository class has a materials class inside it.. Is it a good practise or i have to move it out... Any suggestion..
Pandiya Chendur
@Pandiya: I would separate the domain model (e.g. Materials class) from the repository.
M4N