【问题标题】:Displaying an error message from my controller, and redirect back to my homepage显示来自我的控制器的错误消息,并重定向回我的主页
【发布时间】:2016-02-17 14:05:42
【问题描述】:

我目前正在发出从我的视图“Index.cshtml”发送的数据库查询请求,但是如果用户输入的选择没有返回任何结果,我会尝试向用户发送消息。目前,我找到了一个示例,该示例为用户提供了一个 JS 框,当他们的查询返回为空时向他们显示一条消息。但是,当他们收到消息时,会将他们重定向到空白页面。

我的目标是 当他们的查询请求返回为空时,让用户收到一条消息。并且只需重新加载网页,以便他们重试查询。这是我拥有的以下代码。

代码

        public ActionResult GetClientList(int? marketGroup, int? engagementOffice, int? engagementpartner, int? engagementStatus)
    {
        List<Engagement> QueryResult = PMService.GetRequestedEngagments(marketGroup, engagementOffice, engagementpartner, engagementStatus); 

        if(QueryResult.Count==0)
        {                
            return View("<script language='javascript' type='text/javascript'>alert('Your Search Came Back Empty Please Retry ');</script>");
        }


        var writetofile = PMService.BuildCsvString(QueryResult);
        var bytefile = Encoding.UTF8.GetBytes(writetofile);


        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        Response.BinaryWrite(bytefile);
        Response.Flush();
        Response.End();

        return View();

    }

目前我的情况是如果列表返回空并给出消息,但之后会将它们带到空白页面。

【问题讨论】:

    标签: javascript c# model-view-controller


    【解决方案1】:

    Response.End() 是你得到一个空白页面的原因,你可能想要重定向而不是 Response.Redirect("/")

    【讨论】:

    • 你看到的已经超出了我的条件,消息是从我的条件返回的,并且永远不会上线。
    【解决方案2】:

    脚本缺少重定向语句。 您可以添加:

     window.location.assign("http://www.w3schools.com");
    

    在脚本标签中,将浏览器引导到新页面。

    【讨论】:

    • 好的,把它放在我的脚本中,但没有运气
    • 如果您正在呈现整页​​(postBack),那么您可能不需要显示警报 - 而只是静态 html 视图 - 您的搜索没有结果?认为您正在将请求作为 ajax 调用处理。
    • Ajax 在我恢复 CSV 下载时表现不佳,所以我继续提交表单。
    • 好的,所以您可以设置一个隐藏的输入字段 - 我们将其命名为:searchStatus,然后在页面上有一个 javascript 将检查值,显示消息 - 这对您有用吗?
    【解决方案3】:

    第一次使用

    return File(filedata, contentType);
    

    从内存流中返回文件或服务器磁盘上的文件

    要检查文件是否包含任何数据,您可以先执行 Ajax 请求以检查将返回的记录数。如果你得到 0 只是提醒用户,如果你得到更多的 0 将用户重定向到返回文件的 Actionresult。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多