从部分视图发布Ajax后的消息

时间:2013-07-12 06:49:19

标签: c# jquery asp.net-mvc fancybox asp.net-mvc-partialview

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

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

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

_SendSms

$('#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);
    }

AddAntiforgeryToken功能:

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

1 个答案:

答案 0 :(得分:0)

因为您没有禁止按钮的默认行为:

$('#sendBtn').click(function (e) { 
    e.preventDefault();
    // rest of the handler here
}