如何在 razor @helper 中使用 HtmlHelper 和 AjaxHelper?
我想创建一个助手:
@using System.Web.Mvc
@using System.Web.Mvc.Ajax
@using System.Web.Mvc.Html
@using System.Web.Routing
@using Proj.Extenders
@using Proj.Web.Mvc
@using Proj.Web.Mvc.Paging
@using Proj.Mvc.Helpers
@using Proj.Mvc
@helper Create(dynamic ajaxHelper, dynamic htmlHelper,
string text,
string targetID,
string actionName,
string controllerName,
object routeValues,
string active)
{
<li@{ if (active == targetID) { <text> class="active"</text> } }>
@ajaxHelper.ActionLink(htmlHelper.Resource(text).ToString(),
actionName, controllerName, routeValues, updateTargetId: targetID)
</li>
}
问题是我在 HtmlHelper
上有扩展方法,其中 T
是模型的类型。我想对多个视图使用相同的帮助器,但视图上的 @model
会有所不同。
它没有找到扩展方法:
'System.Web.Mvc.AjaxHelper<X>' does not contain a definition for 'ActionLink'
ActionLink
方法
public static MvcHtmlString ActionLink<T>(this AjaxHelper<T> ajax,
string linkText = " ",
string actionName = null, string controllerName = null,
object routeValues = null, object htmlAttributes = null,
string confirm = null,
string httpMethod = null,
InsertionMode insertionMode = InsertionMode.Replace,
int loadingElementDuration = 0,
string loadingElementId = null,
string onBegin = null,
string onComplete = null,
string onFailure = null,
string onSuccess = null,
string updateTargetId = null,
string url = null)
{
var options = GetAjaxOptions(confirm, httpMethod, insertionMode, loadingElementDuration,
loadingElementId, onBegin, onComplete, onFailure, onSuccess, updateTargetId, url);
return ajax.ActionLink(linkText, actionName, controllerName, routeValues, options, htmlAttributes);
}
该文件位于
- Website
- App_Code
TabHelper.cshtml
如何使用 HtmlHelper
和 AjaxHelper
方法在帮助文件上?
I want to create a helper:
@using System.Web.Mvc
@using System.Web.Mvc.Ajax
@using System.Web.Mvc.Html
@using System.Web.Routing
@using Proj.Extenders
@using Proj.Web.Mvc
@using Proj.Web.Mvc.Paging
@using Proj.Mvc.Helpers
@using Proj.Mvc
@helper Create(dynamic ajaxHelper, dynamic htmlHelper,
string text,
string targetID,
string actionName,
string controllerName,
object routeValues,
string active)
{
<li@{ if (active == targetID) { <text> class="active"</text> } }>
@ajaxHelper.ActionLink(htmlHelper.Resource(text).ToString(),
actionName, controllerName, routeValues, updateTargetId: targetID)
</li>
}
The problem is that I have extension methods on the HtmlHelper<T>
where T
is the type of the model. I want to use the same helper for several views, but the @model
on the views will be different.
It is not finding the extension method:
'System.Web.Mvc.AjaxHelper<X>' does not contain a definition for 'ActionLink'
ActionLink
method
public static MvcHtmlString ActionLink<T>(this AjaxHelper<T> ajax,
string linkText = " ",
string actionName = null, string controllerName = null,
object routeValues = null, object htmlAttributes = null,
string confirm = null,
string httpMethod = null,
InsertionMode insertionMode = InsertionMode.Replace,
int loadingElementDuration = 0,
string loadingElementId = null,
string onBegin = null,
string onComplete = null,
string onFailure = null,
string onSuccess = null,
string updateTargetId = null,
string url = null)
{
var options = GetAjaxOptions(confirm, httpMethod, insertionMode, loadingElementDuration,
loadingElementId, onBegin, onComplete, onFailure, onSuccess, updateTargetId, url);
return ajax.ActionLink(linkText, actionName, controllerName, routeValues, options, htmlAttributes);
}
The file is located at
- Website
- App_Code
TabHelper.cshtml
How can I use HtmlHelper<T>
and AjaxHelper<T>
methods on the helper file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内联
@helper
不能是通用的。您将必须使用定义为扩展方法的标准帮助器,或者只是一些编辑器/显示模板或部分视图。在您的特定情况下,我不明白为什么您的自定义ActionLink
必须是通用的。您最终会在最后调用ajax.ActionLink
,这不是通用的。Inline
@helper
cannot be generic. You will have to use either a standard helper defined as an extension method or or simply some editor/display template or a partial view. This being said in your particular case I don't see why your customActionLink
must be generic. You end up callingajax.ActionLink
at the end which is not generic.