Search code examples
asp.net-mvctelerikasp.net-mvc-3razoreditortemplates

Error creating EditorTemplates in Razor


I have a perfectly working ascx editor template in ASP.NET MVC 3, and tried to convert it to razor:

Ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Inventory.Models.ProductCategory>" %>

<%= Html.Telerik().DropDownList()
    .Name("ProductCategory")
        .BindTo(new SelectList((IEnumerable)ViewData["ProductCategories"], "Id", "Name"))
%>

Razor:

@inherits  System.Web.Mvc.ViewUserControl<Inventory.Models.ProductCategory>

@(Html.Telerik().DropDownList()
    .Name("ProductCategory")
        .BindTo(new SelectList((IEnumerable)ViewData["ProductCategories"], "Id", "Name"))
)

I renamed the ascx so it wouldn't clash when ASP.NET is picking the editor template, I saved the razor file with a cshtml extension, all that. But in runtime, I get this error:

CS0115: 'ASP._Page_Views_Shared_EditorTemplates_ProductCategory_cshtml.Execute()': no suitable method found to override

Line 44:         }
Line 45:         
Line 46:         public override void Execute() {
Line 47: 
Line 48: WriteLiteral("\r\n");

What am I doing wrong? Aren't Razor EditorTemplates recognized by ASP.NET MVC?


Solution

  • Razor views cannot inherit from ViewUserControl. Instead you want to just specify the model of your Razor view:

    @model Inventory.Models.ProductCategory
    
    @(Html.Telerik().DropDownList()
          .Name("ProductCategory")
          .BindTo(new SelectList((IEnumerable)ViewData["ProductCategories"], "Id", "Name"))   )