Reputation: 912
I am getting a run time exception as shown below, compiling the source code doesn't show any issues. The issue is not happening for @html.TextBoxFor(), why is this happening for DropDownListFor?
@Html.DropDownListFor(m => m.EView.EStatusId, @Model.EView.EStatus)
public class EView
{
#region Constructor
public EView()
{
this.EDataView = new EDataViewModel();
}
#endregion
#region Properties
public int EStatusId { get; set; }
public EDataViewModel EDataView { get; set; }
public IEnumerable<SelectListItem> EStatus
{
get {
SelectListItem item = null;
List<SelectListItem> status = null;
status = new List<SelectListItem>();
item = new SelectListItem();
item.Text = "Open";
item.Value = "1";
status.Add(item);
item = new SelectListItem();
item.Text = "Closed";
item.Value = "2";
status.Add(item);
return status;
}
}
#endregion
}
System.Web.Mvc.HtmlHelper<EApp.ViewModels.AdminViewModel>
does not contain a definition for DropDownListFor
and the best extension method overload System.Web.Mvc.Html.SelectExtensions.DropDownListFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)
has some invalid arguments
Upvotes: 3
Views: 5321
Reputation: 1330
A bit late I know, but how about dropping the '@' in front of Model.EView.EStatus?
So:
@Html.DropDownListFor(m => m.EView.EStatusId, Model.EView.EStatus)
Upvotes: 0
Reputation: 1082
This error can be caused by using the wrong SelectListItem type. I inadvertently selected the wrong namespace when I hit Ctrl-. on my unresolved SelectListItem type declaration. I selected System.Web.WebPages.Html instead of System.Web.Mvc. This resulted in the semi-cryptic run-time error in the View. The solution is to make sure you are using the correct SelectListItem type in your Controller, ViewModel, etc., by either using the correct namespace in your using statement, or by fully qualifying the SelectListItem type, if needed. (Hopefully it is not needed...)
Upvotes: 4