2011-02-07 4 views
0

私はカスタムHtmlHelperを持っています。MVC:クライアントサイドスクリプト関数を使用するようにHtmlHelperをリファクタリングできない

public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave) 
{ 
    string url = string.Format("/Payroll/Delete?_employeeOtherLeaveId={0}", _leave.LeaveId.ToString()); 
    return html.RouteLink(linkText, "Default", 
     new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" }, 
     //new { @class = "delete-link" } 
     new { onclick = "$.ajax({url: this.href, type: 'DELETE', success: function(result) {$('#wholepage').html(result);}}); return false;" } 
} 

ご覧のとおり、私のRouteLinkコマンドのonclickイベントには長い文字列があります。 私はこれを控えめなJavascriptに入れて、確認ポップアップのためにいくつか余分なJavaScriptを入れたいと思っています。私は、onclickイベントをコメントアウトし、クラスをアンコメント場合 だから、私は、Javascriptの機能を、次の代わりに使用する

$('#delete-link').click(function() { 
    var flag = confirm('Are you sure you wish to delete this item?'); 
    if (flag == true) { 
     $.ajax({ 
      url: this.href, 
      type: 'DELETE', 
      success: function (result) { 
       // update some DOM element with the result returned by the 
       // server. So supposing that you have some <div id="someContainer"> 
       // that will contain the part of the DOM you want updated: 
       $('#wholepage').html(result); 
      } 
     }); 
    } 
    return false; 
}); 

問題、エラーを取得するということです。 System.Web.HttpException:パブリックアクションメソッド 'Delete'がコントローラ 'SHP.Controllers.PayrollController'に見つかりませんでした

これはパブリックメソッドなので、どうして私はそれを取得する必要がありますか?

[AcceptVerbs(HttpVerbs.Delete)] 
    public ActionResult Delete(int _employeeOtherLeaveId) 
    { 
     EmployeeOtherLeaf.Delete(_employeeOtherLeaveId); 
     return RedirectToAction("Payroll"); 
    } 

一部の人々はthis.urlが間違った値を持っていることを指摘しています。この場合、Firebugはthis.hrefが未定義であると報告しています。 したがって、以下のようにヘルパーメソッドを変更します。

public static MvcHtmlString DeleteEmployeeOtherLeave(this HtmlHelper html, string linkText, Leave _leave) 
    { 
     string url = string.Format("/Payroll/Delete?_employeeOtherLeaveId={0}", _leave.LeaveId.ToString()); 
     return html.RouteLink(linkText, "Default", 
      new { _employeeOtherLeaveId = _leave.LeaveId, action = "Delete" }, 
      new { onclick = "DeleteRow('" + url + "')" } 
    } 

javascript関数は次のようになります。

function DeleteRow(url) { 
    $.ajax({ 
     url: url, 
     type: 'DELETE', 
     success: function (result) { 
      $('#wholepage').html(result); 
     } 
    }); 
    return false; 
} 

まだ同じエラーが発生します。

+0

クリック機能の 'this.href'の値は何ですか? – JoeyRobichaud

+0

あなたはヘルパーで削除URLを生成していますが、 "url:this.href"を使って、ajax submitのために、この正しい/バグですか? – hazimdikenli

+0

これはバグかもしれません。Firebugを見たとき、this.hrefは定義されていませんでした。しかし、関数にパラメータとしてurlを送り、その代わりに私は同じ応答を得ました。 – arame3333

答えて

0

あなたの問題は、jQueryはAjax経由でDelete呼び出しを行うことをサポートしていないため、削除することを制限しているため、コントローラ上にGETまたはPOSTが実行されていません。

この問題の解決方法についてはこちらを参照してください:この役立ちます

http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery

希望を

追加:私はこれ以上、最新のjQueryのは、それをサポートするが、述べてに見てきました

タイプ文字列デフォルト: 'GET'タイプ リクエスト( "POST"または "GET")、 のデフォルトは "GET"です。注:他のHTTP リクエストメソッド(PUTや DELETEなど)もここで使用できますが、 これらはすべての ブラウザでサポートされていません。

フレーク状ブラウザのサポートの周りのMVCのGESTだから、あなたのAjaxコードに追加したい場合も呼ばれる_methodを削除し、それを削除

の値を与えるフォームに隠しフィールドを追加することであると方法:

$('#delete-link').click(function() { 
    var flag = confirm('Are you sure you wish to delete this item?'); 
    if (flag == true) { 
     $.ajax({ 
      url: this.href, 
      data: {_method: "delete"}, 
      type: 'POST', 
      success: function (result) { 
       // update some DOM element with the result returned by the 
       // server. So supposing that you have some <div id="someContainer"> 
       // that will contain the part of the DOM you want updated: 
       $('#wholepage').html(result); 
      } 
     }); 
    } 
    return false; 
}); 
+0

私は先にこれがうまくいったと書いています。私は自分のコードと混同してしまいました。私は恐れています。今私はこれが動作しないことがわかります。私は同じエラーを取得します。間違いなく、私は確認メッセージボックスでキャンセルをクリックすると同じエラーが発生します。それはなぜでしょうか? – arame3333

+0

event.preventDefault();を追加することをお勧めします。クリックイベントではリンクがhrefでfalseを返してもブラウザを停止するには必ずしも十分ではありません – Richard

+0

削除を制限しているので、DELETEアクションを実行していないと思いますが、元のコメントはそのまま残ります。[AcceptVerbs( HttpVerbs.Delete)]これをテストする – Richard

関連する問題