In addition, our p
Q: In what order
Category: Internet
When I read about
What do you think?
Phenomenon and cli
WILT is a digital
/** * Licensed to
Sacred Heart Schoo
Q: PHP/MySQL - Ho

The present invent
As the Trump admin

The present invent
1. Field of the In
"We've had a lot o
Familial amyotroph
The B.C. governmen
#pragma once #inc
The present invent
Q: How to make a nested foreach loop in Razor MVC3 I have been trying for a while to figure out how to do a nested foreach loop in Razor to get data from the database to display in a form. For example, with this code, I am able to get all the data for the year using @foreach (var item in Model). var yearList = Model.Where(i => i.year == 2009).ToList(); However, I am unable to get data for the Month or the Year or Days of the month, since there is no foreign key relationship between Month or Year and Person which is the model that I am using. Model public class Person { public string firstName; public string lastName; public int age; public string gender; public string stateProvince; public int year { get; set; } public int month { get; set; } public int day { get; set; } public DateTime? getdate; public string[] dayList { get; set; } public string[] monthList { get; set; } } View @using (Html.BeginForm("Index", "Home", FormMethod.Get)) { @Html.DropDownList("Year", new SelectList(yearList, "year")) @Html.DropDownList("Month", new SelectList(yearList, "month")) @Html.DropDownList("Day", new SelectList(yearList, "day"))
Date :
@Html.TextBoxFor(m => m.getdate)
Gender :
} So in a way I need this to loop through Month and Year first, and then loop through the Days within that Month. I tried doing it using the below code but it isn't giving me any results. @foreach (var item in Model) { foreach(var item in Model) { @Html.DisplayText(item.name) } } Thank you in advance. A: First of all you are not displaying any monthlist in your razor page. Secondly you have not set any id attribute in the form. And if you use for loop then you need to use tag. So try the following code:- View:- @model MvcApplication13.Models.Person @using (Html.BeginForm("Index", "Home", FormMethod.Get)) { @Html.DropDownList("Year", new SelectList(yearList, "year")) @Html.DropDownList("Month", new SelectList(yearList, "month")) @Html.DropDownList("Day", new SelectList(yearList, "day"))
Date :
@Html.TextBoxFor(m => m.getdate)
Gender :
@foreach(var item in Model.dayList) { foreach(var item in Model.yearList) { @Html.DisplayText(item.name) } }
} Controller:- public ActionResult Index(int year, int month, int day) { var model = new MyViewModel(); model.yearList = GetYear(year); model.monthList = GetMonth(month); model.dayList = GetDay(day); return View(model); } public List GetMonth(int year) { var model = new List(); DateTime dt = new DateTime(year, 1, 1); DateTime dt1 = new DateTime(year, 12, 31); for (int i = 1; i <= 12; i++) { if (dt1.AddMonths(i - 1).Year != dt.AddMonths(i - 1).Year) { if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) model.Add(dt.AddMonths(i - 1).Month); } } return model; } public List GetDay(int year) { var model = new List(); DateTime dt = new DateTime(year, 1, 1); DateTime dt1 = new DateTime(year, 12, 31); for (int i = 1; i <= 31; i++) { if (dt1.AddMonths(i - 1).Month != dt.AddMonths(i - 1).Month) { if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) model.Add(dt.AddMonths(i - 1).Day); } } return model; } public List GetYear(int year) { var model = new List(); DateTime dt = new DateTime(year, 1, 1); DateTime dt1 = new DateTime(year, 12, 31); for (int i = 1; i <= 12; i++) { if (dt1.AddMonths(i - 1).Year != dt.AddMonths(i - 1).Year) { if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) model.Add(dt.AddMonths(i - 1).Year); } } return model; } So basically when the user select the year and month you need to generate the drop downs for that year and month for which you need to create the view model class. And the corresponding day and year data will be in the respective month and year collection.