Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am Learn MVC create a model then write a linq query then show as belows......

C#
var record = (from TStud in db.tbl_StudentForm
                         where TStud.stdfrm_id == id
                         select new
                         {                           
                             TStud.stdfrm_fh_firstname,
                             TStud.stdfrm_fh_middlename,
                             TStud.stdfrm_occupation,
                             TStud.stdfrm_mo_firstname,
                             TStud.stdfrm_mo_middlename,
                             TStud.stdfrm_mo_lastname
                         }).ToList();


and return record to view

and add view name space is are as belows.......
VB.NET
@model IEnumerable<MCAER_MVC.Models.tbl_StudentForm>



but show the error.....plz help me


The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType4`27[System.Nullable`1[System.Int32],System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1[System.Int32],System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1[System.Int32],System.Nullable`1[System.Int32],System.Nullable`1[System.Int32],System.String,System.String,System.String,System.String,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable



thanks in advance

What I have tried:

var record = (from TStud in db.tbl_StudentForm
where TStud.stdfrm_id == id
select new
{
TStud.stdfrm_fh_firstname,
TStud.stdfrm_fh_middlename,
TStud.stdfrm_occupation,
TStud.stdfrm_mo_firstname,
TStud.stdfrm_mo_middlename,
TStud.stdfrm_mo_lastname
}).ToList();



this is my query to tried
Posted
Updated 22-Sep-16 22:13pm

1 solution

When you do "select new{ .. }" in your query you are creating an anonymous type, and anonymous types can't be passed elsewhere, such as to functions or to a view. Your view expects a list of tbl_StudentForm objects but that's not what you're sending. You need to change your linq query to return actual tbl_StudentForm classes and not anonymous types.

C#
var record = (from TStud in db.tbl_StudentForm
                         where TStud.stdfrm_id == id
                         select TStud).ToList();


Basic LINQ Query Operations (C#)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900