User-578960655 posted
Hi, i want to know how to listed/retrieve data from selected database table with foreach loop, but without showing ALL data instead i want to show selected data only.
Example on html syntax, call it MenuList.cshtml
@foreach (var item in Model.Menu)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Breakfast)
</td>
DbController.cs
public static List<Menu> LoadMenu()
{
string sql = @"select MenuID, Breakfast from dbo.Menu;";
return SqlDataAccess.LoadData<Menu>(sql);
}
MenuList.cshtml.cs
public ActionResult ViewMenu()
{
ViewBag.Message = "Menu List";
var data = LoadMenu();
List<Menu> menu = new List<Menu>();
foreach(var row in data)
{
menu.Add(new Menu
{
Breakfast = row.Breakfast
});
}
return Page();
}
In Menu.cs, i provided 3 data in Breakfast attribute. I would like to show only 1 from 3 data. How should i limit the foreach loop?