I am completely new to MVC forum. I have a list like this.
public class DomainList
{
public string Domain { get; set; }
}
var DomainList = new List<DomainList>();
Now using foreach loop I am adding string value into this list.
var DomainList = new List<DomainList>();
DirectoryEntry en = new DirectoryEntry();
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher(en, "objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach (object domainName in resultPropColl["name"])
{
DomainList.Add(new DomainList { Domain = "LDAP://" + domainName.ToString() });
} }
}
This list (DomainList) I want to bind with the dropdown. How can I achieve this?
Doing this with a simple table (which looks like a GridView is very simple, which I am able to achieve using this code.
This list (DomainList) I want to bind with the dropdown. How can I achieve this?
You can assign the populated domainlist to a ViewBag like given below
var DomainList = new List<DomainList>();
DirectoryEntry en = new DirectoryEntry();
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher(en, "objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach (object domainName in resultPropColl["name"])
{
DomainList.Add(new DomainList { Domain = "LDAP://" + domainName.ToString() });
}
}
//Assign the populated values to a viewbag
ViewBag.domainListItems = DomainList;
}
In view then assign the ViewBag values to dropdownlist
@Html.DropDownList("domainListItems", "Select a Value")
@Html.DropDownList("domainListItems", "Select a Value")
In this section of code, What should I mention here? When I mention "DomainListItems" it throws an exception stating
"Exception Details: System.InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'DomainListIteams'."
var DomainList = new List<DomainList>();
DirectoryEntry en = new DirectoryEntry();
// Search for objectCategory type "Domain"
DirectorySearcher srch = new DirectorySearcher(en, "objectCategory=Domain");
SearchResultCollection coll = srch.FindAll();
List<SelectListItem> listitems = new List<SelectListItem>();
// Enumerate over each returned domain.
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach (object domainName in resultPropColl["name"])
{
//Populate the listitems collections here
listitems.Add(new SelectListItem
{
Text = "LDAP://" + domainName.ToString(),
Value = "LDAP://" + domainName.ToString()
});
}
}
//Assign the populated values to a viewbag
ViewBag.domainListItems = listitems;
}
<tbody>
<td>
@Html.DropDownList("SearchDropdown", "Select a Value")
</td>
</tbody>
Now the only exception that is coming is
Exception Details: System.InvalidOperationException: The ViewData item that has the key 'SearchDropdown' is of type 'System.Collections.Generic.List`1[[Application.Models.DomainList, Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' but must be of type 'IEnumerable<SelectListItem>'.
on this line.
...
Line 13: <tbody>
Line 14: <td>
Line 15: @Html.DropDownList("SearchDropdown", "Select a Value")
Line 16: </td>
Line 17: </tbody>
It worked. Thanks a lot. Can you suggest me some good detailed tutorials or books (PDF of eBook), that gives some basic theoritical overview of MVC 4 architecture for someone like me to start with?
Can you suggest me some good detailed tutorials or books (PDF of eBook), that gives some basic theoritical overview of MVC 4 architecture for someone like me to start with?
If you are looking for hands on tutorial on how to get started with MVC. Below are two of best hands on sample tutorial available for MVC
Member
67 Points
222 Posts
DropDownList
Apr 22, 2015 07:08 AM|maverick786us|LINK
I am completely new to MVC forum. I have a list like this.
Now using foreach loop I am adding string value into this list.
This list (DomainList) I want to bind with the dropdown. How can I achieve this?
Doing this with a simple table (which looks like a GridView is very simple, which I am able to achieve using this code.
How can I achieve the same thing with a dropdownlist, just like the way I did with a simple Tr Td in the above example??
All-Star
50841 Points
9895 Posts
Re: DropDownList
Apr 22, 2015 07:39 AM|A2H|LINK
You can assign the populated domainlist to a ViewBag like given below
In view then assign the ViewBag values to dropdownlist
Check this article for more details
Aje
My Blog | Dotnet Funda
Member
67 Points
222 Posts
Re: DropDownList
Apr 22, 2015 07:53 AM|maverick786us|LINK
Thanks for the help. I have one doubt.
In this section of code, What should I mention here? When I mention "DomainListItems" it throws an exception stating
"Exception Details: System.InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'DomainListIteams'."
So what should I mention there?
All-Star
50841 Points
9895 Posts
Re: DropDownList
Apr 22, 2015 08:22 AM|A2H|LINK
You can try with the below code
Aje
My Blog | Dotnet Funda
Member
67 Points
222 Posts
Re: DropDownList
Apr 22, 2015 08:56 AM|maverick786us|LINK
The result is still the same :(. This is the code that I used based on your suggestion
This is the exception :(
All-Star
50841 Points
9895 Posts
Re: DropDownList
Apr 22, 2015 09:57 AM|A2H|LINK
You need to assign the viewbag name to your dropdownlist.
Change your code like given below
Aje
My Blog | Dotnet Funda
All-Star
58144 Points
15646 Posts
Re: DropDownList
Apr 22, 2015 10:54 AM|bruce (sqlwork.com)|LINK
or:
Member
300 Points
119 Posts
Re: DropDownList
Apr 22, 2015 01:51 PM|aliadravi|LINK
See the best ways to bind the dropdown
MVC dropdown binding best ways
MVC DropDownListFor fill on selection change of another dropdown
Member
67 Points
222 Posts
Re: DropDownList
Apr 23, 2015 12:35 AM|maverick786us|LINK
Thanks for the help once agian. This is the final code.
Now the only exception that is coming is
All-Star
50841 Points
9895 Posts
Re: DropDownList
Apr 23, 2015 12:57 AM|A2H|LINK
You are assigning wrong items to ViewBag, you need to assign listitems instead of listDomains.
Change your code like given below
Aje
My Blog | Dotnet Funda
Member
67 Points
222 Posts
Re: DropDownList
Apr 23, 2015 01:03 AM|maverick786us|LINK
It worked. Thanks a lot. Can you suggest me some good detailed tutorials or books (PDF of eBook), that gives some basic theoritical overview of MVC 4 architecture for someone like me to start with?
All-Star
50841 Points
9895 Posts
Re: DropDownList
Apr 23, 2015 01:09 AM|A2H|LINK
You are welcome
If you are looking for hands on tutorial on how to get started with MVC. Below are two of best hands on sample tutorial available for MVC
NerdDinner
Music Store
Also I will provide some additional resources
The best online Tutorial you will find for MVC none other than this site itself
If you are looking for E-Learning Tutorial on how to get started with MVC. Please check the below links
Below are some Video Tutorial links which will help you to get started in MVC
You can also check the below link which points to 25+ Tutorials and Articles
Aje
My Blog | Dotnet Funda