Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now