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 对象时,它始终为空。简单的 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 回答