c# - 重定向到页面并在 asp.net 中显示弹出窗口
问题描述
我的网站上有一个搜索栏,但如果搜索到的词无效,我会执行 if 语句将用户重定向到主页:
if (jsonResponse is null)
{
return RedirectToAction("Index", "Home");
}
我希望我的网站在我的控制器上进行此检查后重定向到主页,而不是在主页中显示一个弹出窗口,上面写着“你搜索了一个无效的词”
解决方案
你可以试试这个方法
搜索页面控制器
public IActionResult SearchPage()
{
var data = TempData["serachResultFromDb"];
if (data != null)
{
ViewData["serachResultFromDb"] = JsonConvert.DeserializeObject<List<PrinterJob>>(TempData["serachResultFromDb"].ToString());
}
return View();
}
搜索页面 Chtml
@using Newtonsoft.Json.Linq
@using msPartnerSupport.Models
@{
ViewData["Title"] = "Search Page";
}
<h2>@ViewData["Title"]</h2>
@using (Html.BeginForm("SearchKey", "Home", FormMethod.Post))
{
<div>
<table class="table-bordered ">
<tr>
<td><strong>Search</strong></td>
<td>
<input type="text" name="searchkey" placeholder="Enter Search Key" />
</td>
<td>
<input type="submit" value="Search" />
</td>
<td></td>
</tr>
</table>
</div>
<br />
}
<table class="table-bordered ">
<tr>
<th>Printer Name </th>
<th>Total</th>
</tr>
@{
var searkeyResult = (List<PrinterJob>)ViewData["serachResultFromDb"];
if (searkeyResult != null)
{
foreach (var item in searkeyResult)
{
<tr>
<td><strong> @item.PrinterName</strong></td>
<td><strong> @item.TotalPrint</strong></td>
</tr>
}
}
}
</table>
输出应该是这样的:
搜索键控制器
[HttpPost]
public IActionResult SearchKey(string searchkey)
{
//Searching From Database
List<PrinterJob> serachingFromDb = _context.PrinterJobs.Where(skey => skey.PrinterName == searchkey).ToList();
//If no search Result then redirecting to new page
if (serachingFromDb.Count == 0)
{
return RedirectToAction("About");
}
//On successful search result
TempData["serachResultFromDb"] = JsonConvert.SerializeObject(serachingFromDb);
return RedirectToAction("Index");
}
注意:我正在
Home
控制器上进行测试,因此直接重定向到Index
操作。此控制器没有视图页面。
索引控制器
public IActionResult Index()
{
ViewData["Message"] = "You searched for an invalid word";
return View();
}
注意:当没有搜索结果时,它将被重定向。
索引Chtml
@{
ViewData["Title"] = "Index";
}
<div class="container">
<div class="modal fade" id="samplePopup" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title"> </h4>
</div>
<div class="modal-body">
@ViewData["Message"]
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$(function () {
$('#samplePopup').modal('show');
});
});
</script>
没有搜索结果时弹出:
希望它会帮助你。让我知道您是否还有其他顾虑。
推荐阅读
- mapbox - 仅使用 fetch API 从 mapbox 获取建筑物多边形点
- python-3.x - 在 IBM SPSS Statistics 版本 26 中安装或导入 Pandas
- firebase - 新创建的 Firebase 函数引发 UNAUTHENTICATED 错误
- reactjs - 如何在使用 reactjs 提交方法时一次发布 2 个 API?
- android - 使用自定义分辨率录制 Camera2 视频?
- reactjs - 在 404 上反应无限循环
- python - Imdbpy 获取家长指南
- c++ - 无法捕获异常 std::stoi
- c++ - 为什么它会打印出意外的值?
- python - 即使安装了 Matplotlib,它也不会导入。使用视觉工作室代码