你可以这样做:
public static class AjaxExtensions
{
public static IHtmlString DefaultLink(this AjaxHelper helper,string text,string action,string controller,string updateTargetId = "",string onSuccess = "")
{
// Build your link here eventually using
// the arguments passed
var options = new AjaxOptions
{
OnSuccess = onSuccess,UpdateTargetId = updateTargetId,OnFailure = "handleError",LoadingElementId = "loading"
// etc...
}
// return a normal ActionLink passing your options
return helper.ActionLink(text,action,controller,options);
}
}
注意我在签名中使用可选参数从多重重载的灵活性中受益,而不会妨碍它们的维护.根据需要展开:)
然后使用它如下:
@Ajax.DefaultLink("home",updateTargetId: "content")