1

私は MVC と Javascript にかなり慣れていません。削除アクションを機能させようとしています。確認のためにJavascript関数を備えたActionLinkを使用しています。JAVascript の確認が機能しません。[キャンセル] を押しても削除アクションが実行されます。さらに、アクションに [HttpDelete] 動詞を使用できないようです: Delete アクションに 2 つのパラメーターを渡す必要がありますが、アクション リンクに @Html.HttpMethodOverride(Http.Delete) を適用した後、URL を検索します。パラメーターは 1 つだけです: id.

これが私のコードです:アクションリンク

 @Html.HttpMethodOverride(HttpVerbs.Delete)
  @Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="confirmDeleteAction()"})

function confirmDeleteAction() {       
    return confirm('Are you sure you want to delete the notification ?');        
}

削除アクション:

   [HttpDelete]
    public ActionResult Delete(int id, int typeId)
    {
        Service.DeleteNotification(id, typeId);

        NewIndexViewModel model = Service.GetNewIndexViewModel();
        return View("Index", model);
    }
4

3 に答える 3

1

これを試して

 @Html.ActionLink(
            "Delete", 
            "Delete", 
            new {id=notification.Id, typeId=notification.TypeId}, 
            new {onclick="return confirmDeleteAction();"})
于 2013-07-26T07:36:06.770 に答える
0

このタイプもできます。

@Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="confirmDeleteAction(" + notification.Id + ")" })


function OnDeleteClick(id) {
                    var ActionID = id;
                    var flag = confirm('Are you sure you want to delete this record?');
                    if (flag) {
                return true;
                }
        else{
        return false;
        }
于 2013-07-26T12:24:20.920 に答える
0

ユーザーがキャンセルをクリックしたときのブラウザのデフォルトの動作をキャンセルする必要があります。これを行うには、次の結果をタグに返しconfirmDeleteAction()ます。<a>

@Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="return confirmDeleteAction()"})

明確にするために、ブラウザに結果を返すreturnキーワードをハンドラーに追加しました-onclickconfirmDeleteAction()

true= リンクのデフォルトの動作を実行する

false= 何もしない

于 2013-07-26T07:35:39.447 に答える