FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 2371|回复: 0

【经验分享】FineUIMvc中使用F.doPostBack条件下载时出错

[复制链接]
发表于 2019-1-21 10:40:57 | 显示全部楼层 |阅读模式
这是来自网友的问题:


这个问题很具有代表性,我们先来看下你的代码逻辑:
  1. @section body {

  2.     @(F.Panel()
  3.         .ID("Panel1")
  4.         .ShowBorder(false)
  5.         .ShowHeader(false)
  6.         .BodyPadding(5)
  7.         .Layout(LayoutType.VBox)
  8.         .IsViewPort(true)
  9.         .Toolbars(
  10.             F.Toolbar()
  11.                 .ID("Toolbar1")
  12.                 .Items(
  13.                     F.Button()
  14.                         .ID("btnDownload")
  15.                         .Icon(Icon.PackageDown)
  16.                         .Text("判断下载")
  17.                         .Listener("click", "onDownloadClick"),
  18.                     F.Button()
  19.                         .ID("btnDownload")
  20.                         .Icon(Icon.PackageDown)
  21.                         .Text("直接下载")
  22.                         .Listener("click", "downloadFile"),
  23.                     F.Button()
  24.                         .ID("btnClose")
  25.                         .Icon(Icon.Cancel)
  26.                         .Text("关闭")
  27.                         .Listener("click", "parent.removeActiveTab();")
  28.                 )
  29.         )
  30.         .Items(
  31.             F.TextBox()
  32.                 .ID("txtContent1")
  33.                 .ShowLabel(false)
  34.                 .Text("内容一"),
  35.             F.TextBox()
  36.                 .ID("txtContent2")
  37.                 .ShowLabel(false)
  38.                 .Text("内容二"),
  39.             F.Label()
  40.                 .Text("F12,点“判断下载”会显示一个错误 =》 Uncaught SyntaxError: Unexpected token :at new Function (<anonymous>) ...")
  41.         )
  42.     )

  43.     <!-- 隐藏表单,用于自定义POST请求 -->
  44.     @using (Html.BeginForm("DownloadFile", "Download", FormMethod.Post, new { id = "myform" }))
  45.     {
  46.         @Html.AntiForgeryToken()
  47.         <input type="hidden" name="hidContent1" />
  48.         <input type="hidden" name="hidContent2" />
  49.     }

  50. }

  51. @section script {

  52.     <script type="text/javascript">

  53.         //判断是否满足下载条件
  54.         function onDownloadClick(event) {
  55.             F.doPostBack({
  56.                 url: '@Url.Action("btnDownload_Click")',
  57.                 fields: 'Panel1',
  58.                 params: { },
  59.                 enableAjaxLoading: false
  60.             });
  61.         }

  62.         //下载文件
  63.         function downloadFile() {
  64.             //自定义POST请求
  65.             var myform = $('#myform');
  66.             myform.find('[name=hidContent1]').val(txtContent1);
  67.             myform.find('[name=hidContent2]').val(txtContent2);
  68.             myform[0].submit();
  69.         }

  70.     </script>
  71. }
复制代码
后台代码:
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult btnDownload_Click(FormCollection fc)
  4. {
  5.         bool success = false;
  6.         if(!string.IsNullOrEmpty(fc["txtContent1"]) ||!string.IsNullOrEmpty(fc["txtContent2"]))
  7.         {
  8.                 success = true;
  9.         }
  10.         return Json(new { result = success });
  11. }
复制代码


页面显示效果如下:



=====我是分割线===============================================================

上述代码虽然可以运行,但是下载后出现JS错误。这里有个关键的地方被忽略了:

F.doPostBack 回发时,后台 必须返回 UIHelper.Result() !

因为 F.doPostBack 是由 FineUIMvc 自行托管的函数,所以使用这一函数必须要遵守这一约定。还要理解的是,UIHelper.Result() 返回到前台的是一段JavaScript 代码,而不是文本或者JSON字符串。

修改后的代码如下:
  1. //判断是否满足下载条件
  2. function onDownloadClick(event) {
  3.         F.doPostBack({
  4.                 url: '@Url.Action("btnDownload_Click")',
  5.                 fields: 'Panel1',
  6.                 params: { },
  7.                 enableAjaxLoading: false
  8.         });
  9. }

  10. [HttpPost]
  11. [ValidateAntiForgeryToken]
  12. public ActionResult btnDownload_Click(FormCollection fc)
  13. {
  14.         bool success = false;
  15.         if(!string.IsNullOrEmpty(fc["txtContent1"]) ||!string.IsNullOrEmpty(fc["txtContent2"]))
  16.         {
  17.                 success = true;
  18.         }
  19.         //return Json(new { result = success });

  20.         if (success)
  21.         {
  22.                 PageContext.RegisterStartupScript("downloadFile();");
  23.         }
  24.         else
  25.         {
  26.                 PageContext.RegisterStartupScript("F.alert('没有满足下载的文件!');");
  27.         }

  28.         return UIHelper.Result();
  29. }
复制代码

运行时效果:






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2024-3-29 18:45 , Processed in 0.045747 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表