2

我有一个问题,我试图在提交表单后在同一页面上显示一条消息,但我的结果总是错误的,因为我在控制器中的操作被调用了两次,当我点击提交按钮时,第一次我的动作被称为数据通过正确的信息传递给动作并保存到数据库中,第二次每个参数都为空,因此返回假。

我想要一种只提交一次并在同一页面中返回适当的 Json 结果而不重定向的方法,我希望有人可以帮助我,这是我的代码:

我试过了:

function PdfHeaderAndFooterManager() {
 $('#submitPdf').ajaxSubmit(function (e) {
        $.ajax({
            url: "/Dashboard/PdfHeaderAndFooterManager",
            dataType: "json",
            type:"POST",
            data: {headerImage: headerImage, footerImage: footerImage},
            success: function (data) {
                if (data.success) {
                    alert(data.message);
                    $('#resultMessage').text(data.message).css("color", "RED");
                }
                else {
                    alert(data.message);
                    $('#resultMessage').text(data.message).css("color", "RED");
                }
            }

        });
       e.preventDefault();
    });
}

我的观点为简单起见删除了一些代码

@using (Html.BeginForm("PdfHeaderAndFooterManager", "Dashboard", FormMethod.Post, new { enctype = "multipart/form-data", id = "formPdfImages" }))
 div id="resultMessage"></div>
}

public ActionResult PdfHeaderAndFooterManager(HttpPostedFileBase headerImage, HttpPostedFileBase footerImage)
    {

       //some code to declare variables


        if (headerImage != null)
        {
            if (!String.IsNullOrEmpty(headerImage.ContentType))
            {
                headerImageContentType = imageHelper.IsValidImageType(headerImage.ContentType);

                if (headerImageContentType)
                {
                    resizedHeaderImage = imageHelper.ResizeImage(headerImage.InputStream);
                }
                else
                {
                    return Json(new { success = false, message = "Please Upload an image* file less than 2GB." });
                }
            }
        }
        if (footerImage != null)
        {
            if (!String.IsNullOrEmpty(footerImage.ContentType))
            {
                footerImageContentType = imageHelper.IsValidImageType(footerImage.ContentType);
                if (footerImageContentType)
                {   
                    resizedFooterImage = imageHelper.ResizeImage(footerImage.InputStream);
                }
                else
                {

                    return Json(new { success = false, message = "Please Upload an image* file less than 2GB." });
                }
            }
        }
        if (P24DataPrincipal.CurrentIdentity != null)
        {
            if (resizedHeaderImage != null || resizedFooterImage != null)
            {
              //add to DB code
                return Json(new { success = true, message = "Image(s) Uploaded Successfully." });

            }
            else
            {
                return Json(new {success = false, message = "Upload atleast 1 image file." });

            }

        }

        return Json("someview");

    }

以上是我的操作,最重要的部分是“返回”关键字我只想看到,在我看来,即使结果为真,不仅结果为假,而且这个操作应该只调用一次而不是重定向。谢谢。

4

3 回答 3

0

你有<input type="submit" />吗?如果是这样,请将其更改为一个按钮并将其连接到您的 jQueryready中,以便它达到您的功能:

$.ready(function(){
    $("#submitButtonId").click(function() {
        //put your ajax stuff here
    });
});
于 2012-05-24T20:33:14.287 回答
0

您的第一个函数实际上看起来接近正确。一些小的调整如下所示。

下面的工作吗?

$(function() {
    $('#formPdfImages').submit(function(event) {
        event.preventDefault();
        event.stopPropagation();

        $.post("@(Url.Action("PdfHeaderAndFooterManager", "Dashboard"))", $(this).serialize(), function(data) {
            if (data.success) {
                $("#resultMessage").text(data.success);
            } else {
                $("resultMessage").text(data.success);
            }

        });
    });
});
于 2012-05-24T20:36:53.963 回答
0

您正在使用@using (Html.BeginForm())并且不能使用提交来进行 Ajax 调用,

对于 ajax 调用,您将不得不使用:@using (Ajax.BeginForm())

但是你需要上传一张图片,所以你必须使用:

@using (Html.BeginForm("PdfHeaderAndFooterManager", "Dashboard", FormMethod.Post, new { enctype = "multipart/form-data", id = "formPdfImages" }))

所以,Ajax 调用是不可能的。要在同一页面上显示消息,请使用另一种选择,可能是:

public ActionResult PdfHeaderAndFooterManager(HttpPostedFileBase headerImage, HttpPostedFileBase footerImage)
    {

   //some code to declare variables


    if (headerImage != null)
    {
        if (!String.IsNullOrEmpty(headerImage.ContentType))
        {
            headerImageContentType = imageHelper.IsValidImageType(headerImage.ContentType);

            if (headerImageContentType)
            {
                resizedHeaderImage = imageHelper.ResizeImage(headerImage.InputStream);
            }
            else
            {
                ViewBag.ReturnMessage="<span style='color:red'> Please Upload an image* file less than 2GB.</span>";
                return View();
            }
        }
    }
    if (footerImage != null)
    {
        if (!String.IsNullOrEmpty(footerImage.ContentType))
        {
            footerImageContentType = imageHelper.IsValidImageType(footerImage.ContentType);
            if (footerImageContentType)
            {   
                resizedFooterImage = imageHelper.ResizeImage(footerImage.InputStream);
            }
            else
            {
                ViewBag.ReturnMessage="<span style='color:red'>Please Upload an image* file less than 2GB.</span>";
                return View();
            }
        }
    }
    if (P24DataPrincipal.CurrentIdentity != null)
    {
        if (resizedHeaderImage != null || resizedFooterImage != null)
        {
          //add to DB code
                ViewBag.ReturnMessage="<span style='color:green'>Image(s) Uploaded Successfully.</span>";
                return View();
        }
        else
        {
            ViewBag.ReturnMessage="<span style='color:red'>Upload atleast 1 image file.</span>";
            return View();
        }

    }

在视图中:

@using (Html.BeginForm("PdfHeaderAndFooterManager", "Dashboard", FormMethod.Post, new { enctype = "multipart/form-data", id = "formPdfImages" })){
 <div id="resultMessage">
    @ViewBag.ReturnMessage
</div>
    }
于 2013-04-03T06:21:24.043 回答