In my Asp.net MVC program, I have an action method in a controller like this. There are two pieces of codes which perform the same functionality. The question is, why the comment codes not work?(I checked the view, the comment codes cannot generate anything I desire to see, while the other can.) I guess maybe it has something to do with the for-loop, because variable i
disappear from memory after the for-loop end. Any thought is of worth, thanks.
//
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register(string code, string state)
{
……
var departments = new List<IEnumerable<SelectListItem>>();
//functionally equal, but not work.
// for (int i=0; i< db.Settings.Find(1).MaxDepartmentLevel;i++)
// {
// departments.Add(ToSelectListItems(db.Departments.Where(r => r.Level == i+1)));
//
// }
//these work.
departments.Add(ToSelectListItems(db.Departments.Where(r => r.Level == 1)));
departments.Add(ToSelectListItems(db.Departments.Where(r => r.Level ==2)));
departments.Add(ToSelectListItems(db.Departments.Where(r => r.Level == 3)));
var registerViewModel = new RegisterViewModel()
{
OpenId = wechatUserAccessToken.openid,
Departments = departments,
AvatarImageUrl = avatarImageUrl.TrimEnd('0') + "96"
};
return View(registerViewModel);
}
Edit:
db.Settings.Find(1).MaxDepartmentLevel
=3.