【问题标题】:redirect to a page and show popup in asp.net重定向到页面并在 asp.net 中显示弹出窗口
【发布时间】:2021-07-12 21:06:07
【问题描述】:

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

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

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

【问题讨论】:

  • 你可以搜索“TempData”
  • 如果您在服务器端...您必须首先呈现一个显示对话框的页面(在 javascript 中)并进行重定向(也在 javascript 中)...服务器端没有访问权限给客户,不能只显示一个弹出窗口......你必须改变一下心态
  • 您可以创建一个额外的视图页面,使用 javascript 显示弹出窗口,当用户单击弹出窗口中的确定按钮时,它将重定向回(索引,主页)
  • 您能否分享您的客户端核心,以便我为您编写解决方案。无法想象那里的实际情况。
  • 这是我的荣幸@lucaspompeun

标签: c# asp.net .net asp.net-mvc asp.net-core


【解决方案1】:

你可以试试这个,

搜索页面控制器

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>

没有搜索结果时弹出:

希望对您有所帮助。如果您还有其他问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多