我在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扩展名保存了剃须刀文件。但是在运行时,我会得到此错误:

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不是吗?

有帮助吗?

解决方案

剃刀视图不能继承 ViewUserControl。相反,您只想指定剃须刀视图的模型:

@model Inventory.Models.ProductCategory

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

其他提示

确保您不是Telerik控件的旧版本,它可能不会针对ASP.NET MVC 3.0(system.web.mvc 3.0汇编)进行编译。还要确保您遵循 文档中描述的说明 对于先决条件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top