显示来自我的控制器的错误消息,并重定向回我的主页

Displaying an error message from my controller, and redirect back to my homepage

本文关键字:我的 重定向 消息 主页 错误 自我 控制器 显示      更新时间:2023-09-26

我目前正在发出从我的视图"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();
    }

目前,我的条件是捕获列表是否为空并给出消息,但之后它会将它们带到空白页。

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

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

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

在脚本标记中,将浏览器定向到新页面。

首次使用

return File(filedata, contentType);

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

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