Reputation: 215
I am getting this error "CS1928:
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownListFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' has some invalid arguments"
My controller looks like this
public class IndexController : Controller
{
public ActionResult Index()
{
EpfeSelectScreen model = new EpfeSelectScreen();
var b = (from a in dbEntitiesErste.CONFIG_APPLICATIONS
orderby a.APPLICATION_ID
select new EPFE.CustomDataObjects.CustomObjects
{
Text = a.APPLICATION_NAME,
Value = a.APPLICATION_ID
});
model.Application = b.OrderBy(x => x.Text).ToList();
return View(model);
}
}
My model is this
public class EpfeSelectScreen
{
public string Search { get; set; }
public string selectedApplication { get; set; }
public List<SelectListItem> Country { get; set; }
public List<CustomObjects> Application { get; set; }
public List<SelectListItem> MetaData { get; set; }
public List<SelectListItem> References { get; set; }
public List<SelectListItem> ReferencedBy { get; set; }
public List<SelectListItem> TreeView { get; set; }
public EpfeSelectScreen()
{
Country = new List<SelectListItem>();
Application = new List<CustomObjects>();
References = new List<SelectListItem>();
ReferencedBy = new List<SelectListItem>();
TreeView = new List<SelectListItem>();
}
}
My CustomObjects is this
public class CustomObjects
{
public string Text { get; set; }
public short Value { get; set; }
}
I have one record in my model.Application, but when that data is passed to my View i get that error.
And my View looks like this
@model EPFE.Controllers.EpfeSelectScreen
@Html.DropDownListFor(m => m.selectedApplication, Model.Application)
How to solve this problem? What am i doing wrong? I get the same error when i try to use ListBoxFor.
Upvotes: 1
Views: 3283
Reputation: 1289
For Adding items to drop down you can use ViewData for passing values to the dropdown.
in the controller: you can have scenario like
IList<SelectListItem> SelectedOptions = new List<SelectListItem>();
IList<Options> DropDownOptions = new List<Options>();
//Add items to this shipping
foreach (var item in DropDownOptions)
{
SelectListItem sliItem = new SelectListItem();
string optionText = item._name;
sliItem.Value = item._value.ToString();
sliItem.Text = optionText;
SelectedOptions.Add(sliItem);
}
ViewData["Option"] = SelectedOptions;
and in the view you can simply use
@Html.DropDownList("Option", ViewData["Option"] as SelectList)
while submitting the form if you are using the class, do remember to put a string property with the name option in the class so that you can use it in the controller also the selected value when the form is submitted.
Upvotes: 0
Reputation: 18411
DropDownListFor
takes a SelectList
as a second argument while you are passing a List
.
You could have something like the following:
@Html.DropDownListFor(m => m.selectedApplication,
new SelectList(Model.Application,"Value","Text"))
Upvotes: 5