c# - Return a list in MVC causes an error -


i trying return list of schedule in education system. have schedule model in project property:

public partial class schedule {     public schedule()     {         this.classtimes = new hashset<classtime>();         this.scores = new hashset<score>();     }      public int id { get; set; }     public int teacherid { get; set; }     public int lessonid { get; set; }     public int classid { get; set; }     public int degreeid { get; set; }     public int facultyid { get; set; }     public int semesterid { get; set; }     public int majorid { get; set; }     public system.datetime dateofexame { get; set; }     public string capacity { get; set; }     public string locationofexame { get; set; }      public virtual class class { get; set; }     public virtual icollection<classtime> classtimes { get; set; }     public virtual degree degree { get; set; }     public virtual faculty faculty { get; set; }     public virtual lesson lesson { get; set; }     public virtual major major { get; set; }     public virtual icollection<score> scores { get; set; }     public virtual semester semester { get; set; }     public virtual teacher teacher { get; set; } } 

so in model save id of entity example major, teacher, lesson, etc. need return list of schedules. have convert id of entity name of entity. design schedule controller in mvc project this:

private readonly schedulerepositor obj = new schedulerepositor();  public actionresult index() {     var list=new list<schedulepresentation>();     classrepository objclassrep=new classrepository();     degreerepositor objdegreerep=new degreerepositor();     facultyrepositor objfactulyrep=new facultyrepositor();     lessonrepository objlessonrep=new lessonrepository();     majorrepository objmajorrep=new majorrepository();     semesterrepositor objsemesterrep=new semesterrepositor();     teacherrepositor objteacherrep=new teacherrepositor();     dateconverter objdateconverte = new dateconverter();     list<schedule> model = obj.getlessonlist();     foreach (var t in model)     {        schedulepresentation objpres=new schedulepresentation();         objpres.capacity = t.capacity;         objpres.dateofexam = objdateconverte.converttopersiantoshow(t.dateofexame);         objpres.classname = objclassrep.findclassbyid(t.classid).classname;         objpres.degreename = objdegreerep.finddegreebyid(t.degreeid).degreename;         objpres.examlocation = t.locationofexame;         objpres.facultyname = objfactulyrep.findfacultybyid(t.facultyid).facultyname;         objpres.lessonname = objlessonrep.findlessonbyid(t.lessonid).lessonname;         objpres.majorname = objmajorrep.findmajorbyid(t.majorid).majorname;         objpres.semestername = objsemesterrep.findsemesterbyid(t.semesterid).semestername;         objpres.teachername = objteacherrep.findteacherbyid(t.teacherid).name + " " +                               objteacherrep.findteacherbyid(t.teacherid).lastname;         list.add(objpres);      }      return view(list); } 

so create repository each entity return name of entity id.

and create representation class schedule convert id name of entities this:

public class schedulepresentation {     public string teachername { set; get; }     public string lessonname { set; get; }     public string classname { set; get; }     public string degreename { set; get; }     public string facultyname { set; get; }     public string semestername { set; get; }     public string majorname { set; get; }     public string dateofexam { set; get; }     public string capacity { set; get; }     public string examlocation { set; get; } } 

so have 2 problems. have 4 projects in solution:

  1. domainclass
  2. model
  3. repository
  4. mvc project

so

  1. is method convert these id name in mvc layer, or better make repository or create model list of schedules?

  2. in last line when want return list got error:

    the model item passed dictionary of type 'system.collections.generic.list1[educationmvc.presentationclass.schedulepresentation]', dictionary requires model item of type 'system.collections.generic.ienumerable1[domainclasses.schedule]'.

the view code of mvc :

@model ienumerable<domainclasses.schedule>  @{     viewbag.title = "index"; }  <h2>index</h2>  <p>     @html.actionlink("create new", "create") </p> <table>     <tr>         <th>             @html.displaynamefor(model => model.teacherid)         </th>         <th>             @html.displaynamefor(model => model.lessonid)         </th>         <th>             @html.displaynamefor(model => model.classid)         </th>         <th>             @html.displaynamefor(model => model.degreeid)         </th>         <th>             @html.displaynamefor(model => model.facultyid)         </th>         <th>             @html.displaynamefor(model => model.semesterid)         </th>         <th>             @html.displaynamefor(model => model.majorid)         </th>         <th>             @html.displaynamefor(model => model.dateofexame)         </th>         <th>             @html.displaynamefor(model => model.capacity)         </th>         <th>             @html.displaynamefor(model => model.locationofexame)         </th>         <th></th>     </tr>  @foreach (var item in model) {     <tr>         <td>             @html.displayfor(modelitem => item.teacherid)         </td>         <td>             @html.displayfor(modelitem => item.lessonid)         </td>         <td>             @html.displayfor(modelitem => item.classid)         </td>         <td>             @html.displayfor(modelitem => item.degreeid)         </td>         <td>             @html.displayfor(modelitem => item.facultyid)         </td>         <td>             @html.displayfor(modelitem => item.semesterid)         </td>         <td>             @html.displayfor(modelitem => item.majorid)         </td>         <td>             @html.displayfor(modelitem => item.dateofexame)         </td>         <td>             @html.displayfor(modelitem => item.capacity)         </td>         <td>             @html.displayfor(modelitem => item.locationofexame)         </td>         <td>             @html.actionlink("edit", "edit", new { id=item.id }) |             @html.actionlink("details", "details", new { id=item.id }) |             @html.actionlink("delete", "delete", new { id=item.id })         </td>     </tr> }  </table> 

your view has typed model this

@model ienumerable<domainclasses.schedule> 

but returning list<schedulepresentation>.

one solution change model line to: @model ienumerable<schedulepresentation>.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -