0

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");
}
Dangerous
  • 4,818
  • 3
  • 33
  • 48
noviscientia
  • 17
  • 1
  • 6
  • try removing the @ from the @Html. you only need the @ if it is in the middle of HTML to tell the code that it needs to be rendered and isn't html – Matt Bodily Jul 07 '14 at 20:33
  • @MattBodily there should be a nugget (`@`) at the `Html.DropDownListFor()` statement since it returns a string. If you don't, the generated string will not be rendered with the output stream. – Henk Mollema Jul 07 '14 at 20:39

2 Answers2

0

I guess that you did not configure the model for the view. Add

@model ddlFor.Models.DropDown

at the top of your view. This makes your view 'strongly-typed', if you don't, the type of Model will be dynamic, which causes your exception. See also: What causes "extension methods cannot be dynamically dispatched" here?

Community
  • 1
  • 1
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
0

Modify your view Index.cshtml as

@Html.DropDownList("ddlSelectID", Model.DDLList,"Please select")

*after then you can bind the value with ID using jquery onchange.

Arun Manjhi
  • 175
  • 9