Skip to content Skip to sidebar Skip to footer

Problems Converting Editorfor To Dropdownlist

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. Tha

Solution 1:

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(
    "", 
    newSelectList(
        new SelectListItem[] 
        { 
            new SelectListItem { Value = "true", Text = "Yes" },
            new SelectListItem { Value = "false", Text = "No" }
        }, 
        "Value", 
        "Text",
        Model
    )
) %>

with the following model:

publicclassMyViewModel
{
    [UIHint("YesNoDropDown")]
    publicbool IsActive { get; set; }
}

controller:

publicclassHomeController : 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>

Post a Comment for "Problems Converting Editorfor To Dropdownlist"