简体   繁体   English

Asp.Net Core 上的 Razor 页面 - 添加了 Razor 页面但无法让页面显示

[英]Razor Pages on Asp.Net Core - Added Razor Page but cant get page to show

I am still figuring a lot out on Razor Pages for.Net Core and am a little stuck on something.我仍然在 Razor Pages for.Net Core 上找到很多东西,并且有点卡住了。 I have added a new Razor Page:我添加了一个新的 Razor 页面:

在此处输入图像描述

Which lives in the Pages folder:位于Pages文件夹中:

在此处输入图像描述

On an existing page I have added an Input and a button to initiate a Search:在现有页面上,我添加了一个输入和一个按钮来启动搜索:

在此处输入图像描述

Which onClick calls the following Javascript function:其中onClick调用如下Javascript function:

在此处输入图像描述

In the below screenshot, OnGetAsync method is hit when I put a breakpoint in, and can see the input value SearchValue is populated:在下面的屏幕截图中,当我在其中设置断点时会触发 OnGetAsync 方法,并且可以看到输入值 SearchValue 已填充:

在此处输入图像描述

However I would expect the browser to then display the Search.cshtml page.但是,我希望浏览器随后显示Search.cshtml页面。 Instead the browser just remains on the current index.cshtml page.相反,浏览器只保留在当前的 index.cshtml 页面上。

Am I missing something or am I just doing this incorrectly?我错过了什么还是我做错了?

However I would expect the browser to then display the Search.cshtml page.但是,我希望浏览器随后显示 Search.cshtml 页面。 Instead the browser just remains on the current index.cshtml page.相反,浏览器只保留在当前的 index.cshtml 页面上。

That is because $.get will not call back the result only if you did something in success function.那是因为$.get只有在您成功执行 function 时才会回调结果。

You need change jquery like below:您需要更改 jquery,如下所示:

@page
@model IndexModel

<input id="inpSearchMembers" placeholder="Search for a Member..." />
<input id="btnSearchMembers" type="button" value="Search"  onclick="SearchMembers()" />
@section Scripts
{
    <script>
        function SearchMembers() {
            var searchValue = $("#inpSearchMembers").val();
            debugger;
            $.get("/Search", { searchValue: searchValue }, function (data) {
                window.location.href = "/Search";   //add this...
            });
        }
    </script>
}

BTW, You create a button which is disabled ,it is impossible to click it and trigger the onclick event.顺便说一句,您创建了一个disabled的按钮,无法单击它并触发 onclick 事件。

Actually,I think you could use form submit without using the jquery,change your Index.cshtml like below:实际上,我认为您可以在不使用 jquery 的情况下使用表单提交,将您的 Index.cshtml 更改如下:

@page
@model IndexModel
<form asp-page="Search" method="get">
    <input name="searchValue" placeholder="Search for a Member..." />
    <input type="submit" value="Search" />
</form>

In your page controller you should have an Action which returns the View:在您的页面 controller 中,您应该有一个返回视图的操作:

public IActionResult Search()
{
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Razor页面更改ASP.Net Core 2.1中的主页 - Changing home page in ASP.Net Core 2.1 with Razor pages Asp.net 核心 Razor 页面,页面处理程序不工作 - Asp.net Core Razor Pages, page handler not working Asp.net core 3.1 with Razor 页面重定向到索引页面而不是预期页面 - Asp.net core 3.1 with Razor Pages redirects to the Index page instead of the intended page Asp.Net Core Razor Pages: asp-route to use value from input on the same page - Asp.Net Core Razor Pages: asp-route to use value from input on same page ASP.NET Core 2.1 Razor 页面返回带模型的页面 - ASP.NET Core 2.1 Razor page return Page with model 如何在 ASP.NET Core Razor Pages 项目的 _layout.cshtml 文件中使用 Razor 页面使用实体框架作为部分视图? - How to use Razor Page Using Entity Framework as a partial view in _layout.cshtml file in ASP.NET Core Razor Pages project? 如何在 ASP.NET 核心 Razor 页面中获得完整的 URL? - How to get FULL URL in ASP.NET Core Razor Pages? ASP.NET Core Razor页面的路由 - Routing for ASP.NET Core Razor Pages 后处理程序上的部分页面模型未执行 ASP.NET Core Razor Pages 2.0 - partial page model on post handler is not executing | ASP.NET Core Razor Pages 2.0 在 ASP.NET Core 3.1 MVC+Razor Pages+Web API 中设置默认页面 - Set default page in ASP.NET Core 3.1 MVC+Razor Pages+Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM