0

私はボタンを持っています:

<a id="2" class="modalInput specialbutton" href="/Employee/Delete/2" rel="#yesno"><img src="/Content/Images/application_delete.png" alt="Delete" /></a>

ボタンのJavascript:

var buttons = $("#yesno button").click(function (e) {
                var yes = buttons.index(this) === 0;
                if (yes) {
                    $.ajax({
                        url: overlayElem.attr('href'),
                        success: function (data) {
                            $("#gridcontainer").html(data);
                        }
                    });
                }
            });

アクションの削除:

public ActionResult Delete(int id)
{
    DeleteTeamEmployeeInput deleteTeamEmployeeInput = new DeleteTeamEmployeeInput { TeamEmployee = id };

    return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
        s => RedirectToAction<EmployeeController>(x => x.Index(1)),
        f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}

問題はidパラメータです。直接使用すると便利ですDeleteTeamEmployeeInput

public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput )
{
    return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
        s => RedirectToAction<EmployeeController>(x => x.Index(1)),
        f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}

Complextオブジェクトを使用すると、常にnullになります。単純なint型は問題なく機能します。

削除アクションに複合型を使用するにはどうすればよいですか?

クラスDeleteTeamEmployeeInput:

public class DeleteTeamEmployeeInput
{
    public int TeamEmployee { get; set; }
}

削除ボタン:

public static string DeleteImageButton(this HtmlHelper helper, int id)
{
    string controller = GetControllerName(helper);
    string url = String.Format("/{0}/Delete/{1}", controller, id);

    return ImageButton(helper, url, "Delete", "/Content/Images/application_delete.png", "#yesno", "modalInput", id);
}
4

1 に答える 1

1

クリックコールバックからfalseを返すことで、デフォルトのアクション結果をキャンセルする必要があります。そうしないと、リダイレクトされる前にAJAXリクエストを実行する時間がない場合があります。オブジェクト全体(単一のTeamEmployee整数プロパティのみを含む)の送信に関する限り、これを行うことができます。

// that selector seems strange as you don't have a button inside your anchor
// but an <img>. You probably want to double check selector
var buttons = $('#yesno button').click(function (e) {
    var yes = buttons.index(this) === 0;
    if (yes) {
        $.ajax({
            url: this.href,
            success: function (data) {
                $("#gridcontainer").html(data);
            }
        // that's what I was talking about canceling the default action
        });
        return false;
    }
});

次に、次のパラメータが含まれるようにアンカーを生成します。

<a href="<%: Url.Action("delete", "employee", new { TeamEmployee  = "2" }) %>" id="link2" class="modalInput specialbutton" rel="#yesno">
    <img src="<%: Url.Content("~/Content/Images/application_delete.png") %>" alt="Delete" />
</a>

今、あなたは安全に持つことができます:

public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput)
{
    ...
}

備考:id="2"アンカー内の識別子名は有効ではありません。

于 2011-01-19T22:34:10.110 に答える