Reputation: 20078
MenuType definiation:
public string Code { get; set; }
public string Name { get; set; }
ASP.NET MVC 5
I have searched and read before I'm posting my question here,
I'm trying to LOAD the data in the asp.net mvc dropdownlist why is that so complicated?
//controller
public class ClientController : BaseController
{
public ActionResult Index()
{
List<MenuType> ctypelist = db.ContractTypes.OrderBy(x => x.TypeOfContract).ToList();
IEnumerable<SelectListItem> list = new SelectList(ctypelist.ToList());
ViewBag.DropDownTypeOfContract = list;
return View();
}
}
//html
@model myapp.Models.Client
@Html.DropDownList("Codes", (List<SelectListItem>)ViewBag.DropDownTypeOfContract , new { style = "max-width: 600px;" })%>
Upvotes: 1
Views: 2577
Reputation: 93434
What's complicated is that you can't seem to decide which type you're using...
First you have a List<MenuType>
(I assume ContractTypes
is actually of type MenuType
?) Then you create a SelectList
, passing the List<MenuType>
to it, which implies that MenuType
must have at least two properties, one called Text
and one called Value
. If not, you will have to specify the Text
and Value
property names in the SelectList
constructor parameters.
After that, for some reason you convert it to a IEnumerable<SelectListItem>
, then you assign that to a ViewBag item and call your View. So, at this point, your ViewBag.DropDownTypeOfContract is of type IEnumerable<SelectListItem>
.
Next, in your View, you for some reason define an @model
depite not passing any model at all to the view. Ok.... Whatever...
So now we get to the real problem.
@Html.DropDownList("Codes",
(List<SelectListItem>)ViewBag.DropDownTypeOfContract ,
new { style = "max-width: 600px;" })%>
Ok, let's ignore for a moment the fact that you have a WebForms closing code block indicator (%>) for some reason... The biggest problem here is that you're trying to cast ViewBag.DropDownTypeOfContract
to a List<SelectListItem>
, which is something it is not, and never was.
You converted the List<MenuType>
to a SelectList
which you then converted to an IEnumerable<SelectListItem>
. There was never any List<SelectListItem>
involved.
So, the simple fix (besides rewriting your code to be sane) is to change your cast as such:
@Html.DropDownList("Codes",
(IEnumerable<SelectListItem>)ViewBag.DropDownTypeOfContract,
new { style = "max-width: 600px;" })
EDIT:
Since your MenuType does not contain the appropriate properties, you will have to modify your SelectList as such (Which I mention above). FYI, ctypelist is already a list, no need to convert it to a list again... that's just silly.
IEnumerable<SelectListItem> list = new SelectList(ctypelist, "Code", "Name");
Upvotes: 2
Reputation: 7328
Note: I have posted this answer without knowledge of what variables your MenuType Class has. Please add to your question and I will edit this answer according to youe MenuType Class
All Dropdowns are a collection of Value and Text Pairs.
<select>
<option value=1>TEXT 1</option>
<option value=2>TEXT 2</option>
<option value=3>TEXT 3</option>
</select>
You have a list of List<MenuType>
, Which values from the MenuType do you want to display in the DropDown List?
Assuming you have this as MenuType.cs
public class MenuType
{
public int MenuTypeId {get;set;}
public string Name {get;set;}
}
Your dropDown should be generated like this:
public ActionResult Index()
{
Dictionary<int,string> ctypelist = db.ContractTypes.OrderBy(x => x.TypeOfContract).ToDictionary(s => s.MenuTypeId, s=> s.Name);
IEnumerable<SelectListItem> selectListItems = ctypelist.Select(s => new SelectListItem() { Value = s.Key.ToString(), Text = s.Value });
ViewBag.DropDownTypeOfContract = selectListItems;
return View();
}
In View:
@{
var items = (IEnumerable<SelectListItem>) ViewBag.DropDownTypeOfContract;
}
@Html.DropDownList("Codes", items , "Select Item")
Upvotes: 1