I am trying to make a "DropDownlist" using class input with a "DropDownlistFor" Statement.
I had to add "using System.Web.Mvc"
to the reference list for a "SelectListItem"
to function properly.
There is an error in the line: "@Html.DropDownListFor(m => m.Id, Model.DDLList, "Please select");"
It states: 'System.Web.Mvc.HtmlHelper<dynamic>'
has no applicable method named 'DropDownListFor'
but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
Class1.cs
using System.Web.Mvc;
namespace ddlFor.Models
{
public class Dropdown
{
public string Id { get; set; }
public List<SelectListItem> DDLList
{
get
{
return new List<SelectListItem>()
{
new SelectListItem
{
Text = "Yes",
Value = "1",
Selected = true
},
new SelectListItem
{
Selected = true,
Value = "0",
Text = "No"
}
};
}
}
}
}
HomeController.cs
using ddlFor.Models;
namespace ddlFor.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new Models.Dropdown());
}
}
}
Index.cshtml
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.DropDownListFor(m => m.Id, Model.DDLList, "Please select");
}