我正在尝试构建 AjaxHelper 扩展,但似乎遇到了麻烦。
查看:
<%= Ajax.DeleteLink("Delete", "LicenseDelete", "Directory", new { LicenseID = license.ID }, new { @class = "directory button" }); %>
扩展名:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Web.Routing;
using System.Web.Mvc.Ajax;
namespace RainWorx.FrameWorx.MVC
{
public static class HtmlExtensions
{
public static MvcHtmlString DeleteLink<TModel>(this AjaxHelper<TModel> ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
return ajaxHelper.ActionLink(linkText
, actionName
, controllerName
, routeValues
, new AjaxOptions { Confirm = "Are you sure you want to delete this item?",
HttpMethod = "DELETE",
OnSuccess = "function() { window.location.reload(); }" }
, htmlAttributes);
}
}
}
浏览器结果:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.Compiler Error Message: CS1061: 'System.Web.Mvc.AjaxHelper<RainWorx.FrameWorx.MVC.ViewModels.DirectoryEdit>' does not contain a definition for 'DeleteLink' and no extension method 'DeleteLink' accepting a first argument of type 'System.Web.Mvc.AjaxHelper<RainWorx.FrameWorx.MVC.ViewModels.DirectoryEdit>' could be found (are you missing a using directive or an assembly reference?)
我错过了什么(明显的)事情?我可以发誓我的扩展方法中的第一个参数确实几乎接受我选择扔给它的任何模型。
TIA
-kb
请您参考如下方法:
您可能需要在 web.config 或 View 本身中注册命名空间。
使用 WebForms 查看示例:
<%@ Import Namespace="RainWorx.FrameWorx.MVC" %>
http://msdn.microsoft.com/en-us/library/eb44kack.aspx
使用带有 C# 的 Razor View 引擎查看示例:
@using RainWorx.FrameWorx.MVC
使用带 VB.NET 的 Razor View 引擎查看示例:
@Imports RainWorx.FrameWorx.MVC
How do I import a namespace in Razor View Page?
Web.Config 示例:
<system.web>
<compilation>
<add assembly="RainWorx.FrameWorx.MVC" />
</compilation>
</system.web>
