MVC AjaxHelper Extension - The system cannot find it?
I am trying to build a AjaxHelper extension and seem to be running into a SNAFU.
View:
<%= Ajax.DeleteLink("Delete", "LicenseDelete", "Directory", new { LicenseID = license.ID }, new { @class = "directory button" }); %>
Extension:
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);
}
}
}
Browser result:
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.开发者_如何学JAVAFrameWorx.MVC.ViewModels.DirectoryEdit>' could be found (are you missing a using directive or an assembly reference?)
What (obvious) thing did I miss? I can swear the first argument in my extension method does accept pretty much any model I choose to throw at it.
TIA
-kb
You likely either need to register the namespace in web.config or in the view itself.
View example using WebForms:
<%@ Import Namespace="RainWorx.FrameWorx.MVC" %>
http://msdn.microsoft.com/en-us/library/eb44kack.aspx
View example using Razor View Engine w/ C#:
@using RainWorx.FrameWorx.MVC
View example using Razor View Engine w/ VB.NET:
@Imports RainWorx.FrameWorx.MVC
How do I import a namespace in Razor View Page?
Web.Config example:
<system.web>
<compilation>
<add assembly="RainWorx.FrameWorx.MVC" />
</compilation>
</system.web>
http://msdn.microsoft.com/en-us/library/bfyb45k1.aspx
Include your extension namespace in your view. That's almost always the cause.
精彩评论