1

这是我的视图和控制器的代码

@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 View("someview");

}

有人可以帮助我如何编写一个 jquery 函数来仅显示在我看来上述操作中返回的 Json 结果。谢谢

4

2 回答 2

2

社论

您似乎想发回 JSON 或图像。您可能可以使用 HTML 5 Blob API 做一些有趣的事情(或将图像作为带有 DATA URI 的 JSON 有效负载发送回)。通常,对此没有简单的解决方案,尤其是通过 AJAX。

尾注

假设 jQuery 1.5 或更好:

jQuery.post('/TheController/PdfHeaderAndFooterManager')
   .success(function(d){
        jQuery('#resultMessage').html(d.message).attr('data-action-success',d.success);
   }).error(function(){
      jQuery('#resultMessage').html('There was an error sending data to the server.').attr('data-action-success', false);
   });

然后你可以在 CSS 中做一些有趣的事情,比如:

[data-action-success=true]{
 color: green;
}
[data-action-success=false]{
 color: red;
}

令我震惊的是,您正在返回不同类型的结果。这很少见,我建议您将“显示表单操作(GET 操作)”和“保存表单(POST 操作)”分开。GET 可以返回 HTML 操作结果。POST 总是可以返回 JSON。

于 2012-05-27T20:55:13.697 回答
0
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.ResultMessage="<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.ResultMessage="<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.ResultMessage="<span style='color:green'">Image(s) Uploaded Successfully.</span>";
            return View();
    }
    else
    {
            ViewBag.ResultMessage="<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.ResultMessage
 </div>
}

试试这个,希望对你有帮助。

于 2013-04-03T10:49:10.347 回答