2013-07-05 25 views
0

我已經寫了一個Jquery-Ui對話框來彈出,作爲對特定鏈接的確認。但是,這並沒有正確的重定向到我的刪除頁面。但是,如果我在chrome中打開調試器進行調試,那麼代碼將按預期工作。只有當Chrome調試器打開時,JavaScript重定向才起作用

我發現了同樣的問題,但解決方案似乎不適用於我。這是完全相同的情況。問題在這裏JavaScript redirect not working and works only in Chrome if debugger is turned on

所以,我有我的鏈接

<div id="confirm-dialog"> 
    <div class="dialog-inner"> 
     <p id="confirm-dialog-message"></p> 
    </div> 
</div> 
<a href="/Customer/Delete/73865878346587" class="confirmLink" title="Delete Customer" data-confirm-message="Are you sure you want to delete this customer?">Delete</a> 

我有我的JavaScript

$('.confirmLink').click(function (e) { 
    BodyScrolling(false); 

    var theHref = $(this).attr("href"); 
    var theTitle = $(this).attr("title") == null ? "Confirm..." : $(this).attr("title"); 
    var theText = $(this).attr("data-confirm-message") == null ? "Are you sure?" : $(this).attr("data-confirm-message"); 

    $("#confirm-dialog-message").html(theText); 
    $("#confirm-dialog").parent().css({ position: "fixed" }).end().dialog("open"); 
    $("#confirm-dialog").dialog({ 
     title: theTitle, 
     close: function() { 
      BodyScrolling(true); 
     }, 
     buttons: [ 
     { 
      text: "Yes", 
      class: "mws-button red", 
      click: function() { 
       $("#confirm-dialog").dialog("close"); 
       window.location.href = theHref; 
       return false; 
      } 
     }, 
     { 
      text: "No", 
      class: "mws-button black", 
      click: function() { 
       $("#confirm-dialog").dialog("close"); 
      } 
     }] 
    }); 
    return false; 
}); 

所以,當我點擊我的刪除鏈接,我確實提出與我同是和確認對話框沒有按鈕,css樣式正確,並且已經捕獲鏈接href並將其綁定到「是」按鈕。如果我點擊「否」,我會被踢回去,沒有進一步的發生 - 正確!

如果我點擊是,它應該把我發送到它捕獲的原始href。我已經在重定向之前點擊Yes按鈕時觸發警報(theHref),並且它向我顯示正確的地址(/ Customer/Delete/73865878346587),但重定向不會發生。

當我打開chrome調試器來調試javascript或查看是否有任何錯誤發生,那麼一切按預期工作,並正確地重定向我!

在IE中,它也不起作用。

我已經試過......

window.location.href = theHref 
window.location = theHref 
location.href = theHref 
$(location).attr('href', theHref) 

我也嘗試添加返回false;在我重定向之後。什麼都沒有

我在上面向同一個問題添加的鏈接表示要確保「是」按鈕正在頁面上呈現爲......我的位置。

任何人都可以擺脫任何光線?

+1

什麼其他的瀏覽器?請參閱@ Th3BFG,window.location = url或window.location.assign(url)的響應。 – ronnyfm

+0

我試過Chrome,即8,9,10,Firefox。我已經嘗試了其他建議,但仍然沒有:( – jonhoare

回答

0

好吧,我已經找到了答案。 Javascript是一個紅鯡魚!

我嘗試刪除confirmLink jQuery類,以便鏈接只是一個標準鏈接,直接到我的控制器perofm我的刪除。當我做這個測試時,鏈接完美運行。因此,我表示問題與我的JavaScript。然而,看起來情況並非如此,如果Chrome中的調試器當時已經打開或者已經打開,那麼只能再次運行。

當我再次訪問非確認鏈接選項時,我發現這不能正常工作,因此表示問題不在javascript中。

看來您無法從MVC中的HTML鏈接執行刪除操作。這顯然是由於任何人都可以在Id上執行刪除所涉及的安全風險。我在我的實現中想到了這一點,並添加了代碼來檢查請求來自哪裏,如果它不是來自我的列表頁面,那麼它會拋出一個錯誤並且不會執行刪除。不管我給我的控制器命名了什麼,例如Test,執行我的HTTP GET請求的鏈接都不會觸發這個命令。必須有一些算法確定動作的作用,並阻止您對HttpGet請求執行動作。有關刪除操作的更多信息,請查看此文章http://stephenwalther.com/archive/2009/01/21/asp-net-mvc-tip-46-ndash-donrsquot-use-delete-links-because

您似乎只能通過HTTP Post執行此操作,這意味着要麼使用Ajax.ActionLink,要麼使用表單和提交按鈕。然後,您的操作必須爲HttpPost指定。

如果像我一樣,希望將鏈接保留爲HTML鏈接,那麼您可以執行以下操作,這是我所做的,代碼如下。我保留了我的HTML鏈接,將其設置爲指向我的HttpPost刪除操作。添加了我的confirmLink類來讓jquery綁定我的對話框。我拿起鏈接href並在jquery對話框中設置「是」按鈕來動態創建一個表單,並將方法設置爲發佈並將操作設置爲鏈接href。然後我可以在新的動態創建的表單上調用提交來執行我的發佈到我的刪除操作。

我刪除鏈接

Html.ActionLink("Delete", "Delete", "Caterer", new { id = caterer.Id }, new { @class = "mws-ic-16 ic-delete imageButton confirmLink", @data_confirm_title = "Delete " + caterer.Name, @data_confirm_message = "Are you sure you want to delete this caterer, " + caterer.Name + "?" }) 

我的JavaScript

function LoadConfirm() { 
    $('.confirmLink').click(function (e) { 
     e.preventDefault(); 
     BodyScrolling(false); 

     var actionHref = $(this).attr("href"); 
     var confirmTitle = $(this).attr("data-confirm-title"); 
     confirmTitle = confirmTitle == null ? "Confirm..." : confirmTitle; 

     var confirmMessage = $(this).attr("data-confirm-message"); 
     confirmMessage = confirmMessage == null ? "Are you sure?" : confirmMessage; 

     $("#confirm-dialog").dialog({ 
      autoOpen: false, 
      modal: true, 
      width: 400, 
      closeOnEscape: true, 
      close: function() { BodyScrolling(true); }, 
      title: confirmTitle, 
      resizable: false, 
      buttons: [ 
      { 
       text: "Yes", 
       class: "mws-button red", 
       click: function() { 
        StartLoading(); 
        $(this).dialog("close"); 
        var form = document.createElement("form"); 
        form.setAttribute("method", "post"); 
        form.setAttribute("action", actionHref); 
        form.submit(); 
       } 
      }, 
      { 
       text: "No", 
       class: "mws-button black", 
       click: function() { 
        $("#confirm-dialog").dialog("close"); 
        return false; 
       } 
      } 
      ] 
     }); 
     $("#confirm-dialog #confirm-dialog-message").html(confirmMessage); 
     $("#confirm-dialog").parent().css({ position: "fixed" }); 
     $("#confirm-dialog").dialog("open"); 
    }); 
} 

我的行動

[HttpPost] 
    [Authorize(Roles = "User")] 
    public ActionResult Delete(long id) 
    { 
     //Perform my delete 

     return RedirectToActionPermanent("List"); 
    } 
0

而不是window.location.href = theHref;

你試過window.location.replace(theHref);

回到基礎,嘗試:window.location = theHref;

+0

是的!仍然不適用於我:( – jonhoare

+0

好吧,這裏有一些原因,爲什麼window.location = url不起作用。這裏的想法是,它,應與所有現代瀏覽器 - 他們已禁用JavaScript - 他們的瀏覽器不支持訪問本身只是位置,你應該使用window.location的 - 你應該指定協議(HTTP,即:。。/ /或https://),只需使用mydomain.com即可在某些瀏覽器中將其轉換爲/mydomain.com。 - 確保將完整地址傳遞給window.location函數。 - 等 – Th3BFG

+0

感謝您的回覆。這仍然是開發不是一個錯誤。我在瀏覽器中啓用了JS,我知道該URL工作正常,我期望它是一個相對路徑,因爲它不會在我的域之外的其他地方使用,它只是到我站點中的另一個MVC Action的鏈接。我可以確認Javascript拾取的鏈接href正確無誤。最重要的是,當chrome調試器窗口打開時,這很好地工作。在我作爲我的問題的一部分發布的鏈接中,別人提到他們的按鈕沒有類型,這是他們的問題。這一定與我的佈局有關。 – jonhoare

相關問題