2015-06-22 51 views
4

我想創建一個類似於Ajax.ActionLink的幫助程序。我創建了一個幫手有了一些變化要做到這一點:如何爲Ajax.ActionLink定製幫助程序

@helper AjaxLink(string innerhtml, string href, string targetId) 
{ 
    if (!string.IsNullOrEmpty(innerhtml)) 
    { 
     if (href.Trim() == "#") 
     { 
      <a href="@(href)"> 
       @MvcHtmlString.Create(innerhtml) 
      </a> 
     } 
     else 
     { 
      <a href="@(href)" data-ajax-update="#@(targetId)" data-ajax-mode="replace"> 
       @MvcHtmlString.Create(innerhtml) 
      </a> 
     } 

    } 
} 

幫助我創建像一個鏈接:

<a href="ItemRegister?testTypeId=1" data-ajax-update="#pageId" data-ajax-mode="replace"> 
    <i class="fa fa-sign-out"><span style="right: -47px;" class="icon-bg bg-orange"></span></i><span>Register </span> 
</a> 

但它不工作!它刷新頁面,而不是填充目標

回答

1

你應該把data-ajax屬性到您的<a>標籤:

<a href="@(href)" data-ajax-update="#@(targetId)" data-ajax-mode="replace" data-ajax="true"> 
     @MvcHtmlString.Create(innerhtml) 
</a> 
+0

我做到了,但不是工作呢! –

+0

但它在我的系統上工作得很好 – Behzad

+1

哦,我忘了連接到'jquery.unobtrusive-ajax.js'。你是對的!謝謝 –