我目前正在发出从我的 View “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("/")
关于javascript - 从我的 Controller 显示错误消息,并重定向回我的主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458877/