【问题标题】:ASP.NET MVC: Model Binding and Ajax RequestASP.NET MVC:模型绑定和 Ajax 请求
【发布时间】:2011-01-19 22:24:21
【问题描述】:

我有一个按钮:

<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);
}

【问题讨论】:

    标签: c# asp.net-mvc model-binding value-provider


    【解决方案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" 不是有效的标识符名称。

    【讨论】:

    • 非常感谢达林!据我了解,我需要将路由数据添加到 url。我现在的问题是如何使这个“新{TeamEmployee =“2”}”通用:)。我会更新我的帖子。
    猜你喜欢
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多