0

Please see Darin's solution here .. Converting HTML.EditorFor into a drop down (html.dropdownfor?)

I am not able to make the drop down list work. Can any help with this please. Thank you.

I am getting BC30203 error in my ascx page.

BC30203: Identifier expected. (Line 4 - new[] ).. What do I put in place of model. I tried putting the actual model name and may be I am getting the syntax wrong.. this code goes in the editor template according to the posted solution link above...

Code:

 <%= Html.DropDownList(
    "", 
   new SelectList(
   new[] 
   { 
    new { Value = "true", Text = "Yes" },
    new { Value = "false", Text = "No" },
   }, 
   "Value", 
   "Text",
   Model
  )
  ) %>
Community
  • 1
  • 1
ZVenue
  • 4,967
  • 16
  • 61
  • 92
  • Have you tried? [Solutions][1] [1]: http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist-to-work – tetuje Mar 05 '12 at 14:19

1 Answers1

2

No idea why you are getting such error, the code should work. The following editor template works perfectly fine for me, I have just tested it:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.DropDownList(
    "", 
    new SelectList(
        new SelectListItem[] 
        { 
            new SelectListItem { Value = "true", Text = "Yes" },
            new SelectListItem { Value = "false", Text = "No" }
        }, 
        "Value", 
        "Text",
        Model
    )
) %>

with the following model:

public class MyViewModel
{
    [UIHint("YesNoDropDown")]
    public bool IsActive { get; set; }
}

controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }
}

and view:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" 
%>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.EditorFor(model => model.IsActive) %> 
</asp:Content>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • To start with the first line says.. its a syntax error..<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> – ZVenue Mar 05 '12 at 16:14
  • @ZVenue, are you putting this in an ASCX file? In `~/Views/Shared/EditorTemplates/YesNoDropDown.ascx`? Note that there's a difference between ASCX and ASPX in the WebForms view engine. – Darin Dimitrov Mar 05 '12 at 16:16