2

我在 ASP.NET MVC 3 中有一个完美运行的 ascx 编辑器模板,并尝试将其转换为 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"))
%>

剃刀:

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

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

我重命名了 ascx,这样当 ASP.NET 选择编辑器模板时它不会发生冲突,我保存了带有 cshtml 扩展名的 razor 文件,所有这些。但在运行时,我收到此错误:

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");

我究竟做错了什么?ASP.NET MVC 不识别 Razor EditorTemplates 吗?

4

2 回答 2

12

Razor 视图不能继承自ViewUserControl. 相反,您只想指定 Razor 视图的模型:

@model Inventory.Models.ProductCategory

@(Html.Telerik().DropDownList()
      .Name("ProductCategory")
      .BindTo(new SelectList((IEnumerable)ViewData["ProductCategories"], "Id", "Name"))   ) 
于 2011-01-24T18:55:08.383 回答
0

确保您不是旧版本的 Telerik 控件,可能无法针对 ASP.NET MVC 3.0(System.Web.Mvc 3.0 程序集)进行编译。还要确保您已按照文档中描述的先决条件步骤说明进行操作。

于 2011-01-24T15:35:47.003 回答