Solved

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

Posted on 2014-07-21
1
200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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