--------------------------------------------------------
when enumerable, I can display lists of things. If I want to to display a single thing, like I would on a details page, I use the second model declaration above. however, I cant have both on the same view.
But I would want to display perhaps a list of course, and details for a single course on the same page. How is this done?
Don't think of it in terms of what you can display, rather focus on what your view is representing. The model is the piece of data you are representing, are you representing 1 thing (a single model) or a lot of things (a collection).
To answer your question, you want to display a list of courses and the details for a single course. Well the view in this case is a representation of the collection of courses. So you want it to be an ienumerable.
Just like you can loop through and see all the courses, you can also extract 1 course and show the details for that course. You can use the extension method .First() to get the first element in the collection.
However, if you want to make it so that a user can click on an item in the list and they get more information. This is done via javascript since you have to respond to runtime events. In such cases it's normal to issue a request to a server to fetch data
based on the selected item.
Just make another model with all necessery parameters. You want single course and list of courses? Declare appropriate model
public class SingleCourseModel
{
...
public Course CurrentCourse{get;set;}
public IEnumerable<Course> SimilarCourses{get;set;} // or whatever you need
...
}
or
public class SingleCourseModel
{
...
public long CurrentCourseId {get;set;}
public IEnumerable<Course> SimilarCourses{get;set;}
public Course CurrentCourse
{
get
{
return SimilarCourses.FirstOrDefault(c => c.CourseId == CurrentCourseId);
}
}
...
}
jypelton
Member
186 Points
162 Posts
enumerable or not enumerable
Apr 26, 2012 01:04 AM|LINK
As I understand it, if I want a strongly typed view model, I have to declare it like this in my view
--------------------------------------------------------
@model IEnumerable<ContosoUniversity.Models.Course>
or like this
@model ContosoUniversity.Models.Course
--------------------------------------------------------
when enumerable, I can display lists of things. If I want to to display a single thing, like I would on a details page, I use the second model declaration above. however, I cant have both on the same view.
But I would want to display perhaps a list of course, and details for a single course on the same page. How is this done?
CodeHobo
All-Star
18669 Points
2648 Posts
Re: enumerable or not enumerable
Apr 26, 2012 01:15 AM|LINK
Don't think of it in terms of what you can display, rather focus on what your view is representing. The model is the piece of data you are representing, are you representing 1 thing (a single model) or a lot of things (a collection).
To answer your question, you want to display a list of courses and the details for a single course. Well the view in this case is a representation of the collection of courses. So you want it to be an ienumerable.
Just like you can loop through and see all the courses, you can also extract 1 course and show the details for that course. You can use the extension method .First() to get the first element in the collection.
However, if you want to make it so that a user can click on an item in the list and they get more information. This is done via javascript since you have to respond to runtime events. In such cases it's normal to issue a request to a server to fetch data based on the selected item.
Blog | Twitter : @Hattan
DenisM
Member
124 Points
45 Posts
Re: enumerable or not enumerable
Apr 27, 2012 08:00 AM|LINK
Just make another model with all necessery parameters. You want single course and list of courses? Declare appropriate model
public class SingleCourseModel { ... public Course CurrentCourse{get;set;} public IEnumerable<Course> SimilarCourses{get;set;} // or whatever you need ... }or
public class SingleCourseModel { ... public long CurrentCourseId {get;set;} public IEnumerable<Course> SimilarCourses{get;set;} public Course CurrentCourse { get { return SimilarCourses.FirstOrDefault(c => c.CourseId == CurrentCourseId); } } ... }ossprologix
Member
394 Points
128 Posts
Re: enumerable or not enumerable
Apr 27, 2012 11:04 AM|LINK
use @model IEnumerable<ContosoUniversity.Models.Course>
and iterate over the model