Stuffing an anonymous type in ViewBag causing model binder issues

前端 未结 2 1925
不思量自难忘°
不思量自难忘° 2020-11-28 15:38

can someone tell me what I\'m doing wrong? :-)

I have this simple query:

 var sample = from training in _db.Trainings
              where training.In         


        
2条回答
  •  离开以前
    2020-11-28 16:01

    If you want to send in ViewData For example and don't want to send in model you could use the same could as in the upper answer and in the Controller

    enter code here
    
    
    ViewData[Instractor] = from training in _db.Trainings
                     where training.InstructorID == 10
                     select new Instructor { 
                         Name = training.Instructor.UserName 
                     };
    

    and in the view you need to cast this to

    `IEnumerable`
    

    but to do this you should use

    @model IEnumerable
    

    Then you could do something like this

    IEnumerable Instructors =(IEnumerable)ViewData[Instractor];
    

    then go with foreach

    @foreach (var item in Instructors ) {
        @item.Something
    }
    

提交回复
热议问题