?
Solved

returning one specific item in a for each loop from a model

Posted on 2014-07-21
1
Medium Priority
?
203 Views
Last Modified: 2014-07-21
I have a question on Foreach loop in razor syntax in a ASP.NET MVC application
Right Now I call every row in a model like this, in my view. So in my table I am showing every record of the CourseCode and CourseName columns :


<!-- Courses -->
<div class="display-field">
    <table id="courses">             
            <thead>
                <th>Course Code</th>
                <th>Course Name</th>
            </thead>
        <tbody>
        <%
            foreach (var item in Model.Courses) {
        %>
            <tr>
                <td><%: Html.DisplayFor(model => item.CourseCode) %></td>
                <td><%: Html.DisplayFor(model => item.CourseName) %></td>
            </tr>
        <% } %></tbody>
    </table>
</div>

Open in new window


The key part is this: foreach (var item in Model.Courses)

How would I revise the for each loop so I only show items with CourseName of "Algebra 1"   ?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 21

Accepted Solution

by:
Randy Poole earned 2000 total points
ID: 40209691
<!-- Courses -->
<div class="display-field">
    <table id="courses">             
            <thead>
                <th>Course Code</th>
                <th>Course Name</th>
            </thead>
        <tbody>
        <%
            foreach (var item in Model.Courses) {
            if (model=>item.CourseName.toString().Equals("Algebra 1"))
            {
        %>
            <tr>
                <td><%: Html.DisplayFor(model => item.CourseCode) %></td>
                <td><%: Html.DisplayFor(model => item.CourseName) %></td>
            </tr>
        <% }
        	} %></tbody>
    </table>
</div>

Open in new window


Not sure if you will need the ToString() or not.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question