Using foreach with two types of conditions in c# -
i used dropdown search. text value should different value. so, created 2 types of methods:
list<string> lstroles = new list<string>(); lstroles = _repository.getrolesforfindjobseekers(); list<string> lstfunctions = new list<string>(); lstfunctions = _repository.getfunctionsforrolesfindjobseekers(); list<selectlistitem> selectlistroles = new list<selectlistitem>(); int = 1; foreach (string role in lstroles) { selectlistroles.add(new selectlistitem { text = role, value = role, selected = (i == 0) }); i++; } viewdata["rolesforjobseekers"] = selectlistroles;
lstfunctions
should come in value field. how add this?
you can first combine these 2 lists 1 , loop on list.
var lstcombined = lstroles .zip(lstfunctions, (role, function) => new {role = role, function = function}).tolist(); int = 1; foreach (var item in lstcombined) { selectlistroles.add(new selectlistitem { text = item.role, value = item.function, selected = (i == 0) }); i++; }
Comments
Post a Comment