0

我有一个在 fancybox 中打开的部分视图。当我发布表单时,我想在部分视图中显示验证消息,该部分视图应在fancybox 中打开。

当我使用我的代码时,我没有收到警报消息,我被重定向到:http ://domain.com/TextMessage有人知道 wye 吗?

我被重定向到的页面没有在fancybox中显示,我需要在成功时再次调用它吗?

_发简讯

$('#sendBtn').click(function () {

        var dataArray = $('form').serializeArray();
        var dataObj = {};

        for (var i = 0; i < dataArray.length; i++) {
            dataObj[dataArray[i].name] = dataArray[i].value;
        }

        $.ajax({
            type: "POST",
            url: "/TextMessage/Send",
            data: AddAntiForgeryToken({ salonId: dataObj['SalonId'], toNumber: dataObj['ToNumber'], message: dataObj['Message'] }),
            success: function (respons) {

                // Can't reach.
                alert("klar");
            }
        });
    });
});

渲染表单视图:

public PartialViewResult Index()
    {
        var salon = _customerManager.GetSalon();
        var smsViewModel = new SmsViewModel
                               {

                                   ToNumber = salon.MobileTel,
                                   Message = string.Format("Ni kommer vid första uppstarten av extreme bli frågade om uppgifter. \n Dessa kommer här: \n Databas: {0}.", salon.DatabaseName),
                                   DateSent = DateTime.MinValue,
                                   SentByUser = _securityManager.CurrentUser.Name,
                                   SalonId = salon.Id
                               };

        return PartialView("Partial_Views/_SendSms", smsViewModel);
    }

发送方式:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Send(string salonId, string toNumber, string message)
    {
        var returnValue = false;

        try
        {
            using (var client = new SmsService.SMSServiceSoapClient("SMSServiceSoap"))
            {
                //client.SendSMSGeneric(int.Parse(salonId), "Itsperfect Software Europe AB", toNumber, message, 8);
            }

            returnValue = true;

        }

        catch (Exception ex)
        {
            Log.Error("Error trying to get Salons withId", ex);
            returnValue = false;
        }

        return Json(new { success = returnValue }, JsonRequestBehavior.AllowGet);
    }

添加AntiforgeryToken函数:

function AddAntiForgeryToken(data) {
    data.__RequestVerificationToken = $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val();
    return data;
};
4

1 回答 1

0

因为您没有抑制按钮的默认行为:

$('#sendBtn').click(function (e) { 
    e.preventDefault();
    // rest of the handler here
}
于 2013-07-12T06:51:16.707 回答