首页 > 解决方案 > 重定向到页面并在 asp.net 中显示弹出窗口

问题描述

我的网站上有一个搜索栏,但如果搜索到的词无效,我会执行 if 语句将用户重定向到主页:

if (jsonResponse is null)
{
    return RedirectToAction("Index", "Home");
}

我希望我的网站在我的控制器上进行此检查后重定向到主页,而不是在主页中显示一个弹出窗口,上面写着“你搜索了一个无效的词”

标签: c#asp.net.netasp.net-mvcasp.net-core

解决方案


你可以试试这个方法

搜索页面控制器

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>

没有搜索结果时弹出:

在此处输入图像描述

希望它会帮助你。让我知道您是否还有其他顾虑。


推荐阅读