enumerable or not enumerablehttp://forums.asp.net/t/1797028.aspx/1?enumerable+or+not+enumerableFri, 27 Apr 2012 11:04:50 -040017970284951709http://forums.asp.net/p/1797028/4951709.aspx/1?enumerable+or+not+enumerableenumerable or not enumerable <p>As I understand it, if I want a strongly typed view model, I have to declare it like this in my view<br> <br> --------------------------------------------------------<br> <br> @model IEnumerable&lt;ContosoUniversity.Models.Course&gt;<br> <br> or like this<br> <br> @model ContosoUniversity.Models.Course<br> <br> --------------------------------------------------------<br> 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.<br> <br> 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?<br> <br> </p> 2012-04-26T01:04:53-04:004951717http://forums.asp.net/p/1797028/4951717.aspx/1?Re+enumerable+or+not+enumerableRe: enumerable or not enumerable <p>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 &nbsp;you representing 1 thing (a single model) or a lot of things (a collection).&nbsp;</p> <p>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.</p> <p>Just like you can loop through and see all the courses, &nbsp;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.</p> <p>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.</p> 2012-04-26T01:15:29-04:004954388http://forums.asp.net/p/1797028/4954388.aspx/1?Re+enumerable+or+not+enumerableRe: enumerable or not enumerable <p>Just make another model with all necessery parameters. You want single course and list of courses? Declare appropriate model</p> <p></p> <pre class="prettyprint">public class SingleCourseModel { ... public Course CurrentCourse{get;set;} public IEnumerable&lt;Course&gt; SimilarCourses{get;set;} // or whatever you need ... }</pre> <p>or</p> <pre class="prettyprint">public class SingleCourseModel { ... public long CurrentCourseId {get;set;} public IEnumerable&lt;Course&gt; SimilarCourses{get;set;} public Course CurrentCourse { get { return SimilarCourses.FirstOrDefault(c =&gt; c.CourseId == CurrentCourseId); } } ... }</pre> <p><br> <br> <br> </p> 2012-04-27T08:00:07-04:004954717http://forums.asp.net/p/1797028/4954717.aspx/1?Re+enumerable+or+not+enumerableRe: enumerable or not enumerable <p>use @model IEnumerable&lt;ContosoUniversity.Models.Course&gt;</p> <p>and iterate over the model</p> <p></p> <p></p> 2012-04-27T11:04:50-04:00