I have just one question in order to understand the prob here :
how come do you have city/cities in students?
what fields are on students ?
I mean, I would have something that look like:
public class Student{
public int StudentId { get; set; }
public string FirstName { get; set;}
// and other student properties
public int CityId { get; set; }
public virtual City City { get; set;}
}
public class City{
public int CityId { get; set;}
public string CityName { get; set;}
}
// Then you query Cities from city entity, then lookup students that belongs to that specific city since you will have CityId foreign key on Student entity
hariharakris...
Member
76 Points
32 Posts
Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 09:13 AM|LINK
Hi all,
I have a collection namely student that has Id, RollNumber, City etc.
I need to load a dropdownlist by passing the selectlist item to the view. i ve done it by using ViewData.My code is below
@Html.DropDownList("ddlCity", new SelectList((System.Collections.IEnumerable)ViewData["ddlCity"], "City", "City"))Here i found duplicate values of City. But i need it only one time.
From the Controller I use to return this...
any one please help me & that would be greatly appreciated. Thanx in advance.
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 09:21 AM|LINK
The view is responsible with display, not with duplicate. For eliminating duplicates, check first how do you load into
kedarrkulkar...
All-Star
34177 Points
5490 Posts
Re: Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 09:26 AM|LINK
try this
ViewData["ddlCity"] = ( from std in students select std ).Distinct();
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
.NetBoy
Member
439 Points
115 Posts
Re: Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 09:39 AM|LINK
I have just one question in order to understand the prob here :
how come do you have city/cities in students?
what fields are on students ?
I mean, I would have something that look like:
public class Student{ public int StudentId { get; set; } public string FirstName { get; set;} // and other student properties public int CityId { get; set; } public virtual City City { get; set;} } public class City{ public int CityId { get; set;} public string CityName { get; set;} } // Then you query Cities from city entity, then lookup students that belongs to that specific city since you will have CityId foreign key on Student entity.NetBoy
Member
439 Points
115 Posts
Re: Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 09:42 AM|LINK
if in students entity, you have a cityId or CityName, then
hariharakris...
Member
76 Points
32 Posts
Re: Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 10:13 AM|LINK
Dear all,
I used a Model Class namely StudentMaster which has ID, RollNumber, StudentName, City.
I binded my view with this Model. when I use distinct like this
ViewData["ddlCity"] = ( from std in students select std ).Distinct(); it returns disctinct based on the identity column ID.
And more on that the City is just a field in the table. not a collection in the model or any other model that is referred into.
hariharakris...
Member
76 Points
32 Posts
Re: Select Distinct values in dropdownlist MVC 3
Jul 05, 2012 10:15 AM|LINK
.NetBoy
It results in an exception