Reputation: 20078
UPDATE:
since its my DropDownTypes
is a collection so I try to pass something like this: it works but the only problem i'm having is that its selecting the default value which is whatever the index of 0 and my item.Type
is the selected value
@Html.DropDownListFor(m => item.Type, new SelectList(item.DropDownTypes.Select(x => x.Text)))
What I'm doing wrong here, I'm trying to load the dropdownlist
'System.Web.Mvc.HtmlHelper>' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable, object)' has some invalid arguments
@model IEnumerable<web.Models.CollectionViewModel>
@foreach (var item in Model )
{
<tr>
<td>
@Html.DropDownList(item.DropDownTypes, (IEnumerable<SelectListItem>)ViewBag.DropDownLoadRecoveryType, new { @class = "form-control" })
</td>
</tr>
}
Upvotes: 4
Views: 9730
Reputation: 20078
This is how I was able to achieve a resolution, in case anybody else needs to know.
@Html.DropDownListFor(model => item.Type.Value, new SelectList(ViewBag.DropDownLoadRecoveryType, "Value", "Text", item.Type))
Upvotes: 4
Reputation: 15934
You should consider adding the selected value of the dropdown list into another variable on your model. That way you can use it in the dropdown list as you're passing a list at present which isn't valid:
@model IEnumerable<web.Models.CollectionViewModel>
@foreach (var item in Model )
{
<tr>
<td>
@Html.DropDownList(item.SelectedDropDownType, (IEnumerable<SelectListItem>)ViewBag.DropDownLoadRecoveryType, new { @class = "form-control" })
</td>
</tr>
}
Where item.SelectedDropDownType
could be
public string SelectedDropDownType { get; set;}
Upvotes: 0
Reputation: 1038930
The first argument of the DropDownList
helper that you are using must be a string so make sure that item.DropDownTypes
is actually a string. If it isn't you might consider calling .ToString()
on it.
Upvotes: 0