Solved

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

Posted on 2014-07-21
1
199 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
1 Comment
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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