【问题标题】:How To Get ASP.NET MVC Form To do same as button submit如何获取 ASP.NET MVC 表单 与按钮提交相同
【发布时间】:2014-06-09 02:07:01
【问题描述】:

我有一个简单的 ASP.NET MVC 表单,其代码如下:

@using (Html.BeginForm(null, null, FormMethod.Post, new
{
  action = "https://secure.authorize.net/gateway/transact.dll",
  id = "registerformid",

}))
{
  @Html.HiddenFor(a => a.RegisterUserInfoLoggedIn.AttendeesId)
  ...

我有一个 JQuery ajax 调用,如果该 JQuery 成功,我希望我的表单 POST 到操作 url 以进行进一步处理。

我的 JQuery 如下所示,但我不知道在我的 JQuery ajax 成功事件中放入什么,以便我回发到操作 url,该页面正确响应并向我显示我的新页面。

               success: function (data) {
                    if (donationAmount > 0.00) {
                        $.post("https://secure.authorize.net/gateway/transact.dll", {
                            x_login: 'xxx',
                            x_amount: 19.99,
                            x_description: 'Sample Transaction',
                            ...

                        }, function(datax) {

                            this.submit();
                        });

【问题讨论】:

标签: jquery asp.net asp.net-mvc asp.net-mvc-2


【解决方案1】:

您可以使用.Submit() 调用表单的提交

success: function (data) {
    $('#registerformid').submit(); // right here.   
}

【讨论】:

    【解决方案2】:

    您可以执行以下操作。

    根据您的操作状态返回正确的 json 格式。

        public ActionResult SomeAction()
        {
            return this.Json(new
            {
                redirectUrl = "/ControllerName/ActionName",
                isRedirect = true
            }, JsonRequestBehavior.AllowGet);
        }
    

    重定向到成功方法提供的页面。

     $.ajax({
        url: '/Home/SomeAction',
        type: 'POST',
        dataType: 'json',
        data: json,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            // redirect to home if true
            if (data.isRedirect) {
                window.location.href = data.redirectUrl;
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-02-10
      • 1970-01-01
      • 2011-02-07
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多