如何在 asp.net-core razor 页面中使用 "get" 部分内容?

How to use partials to "get" stuff in asp.net-core razor pages?

我有以下部分位于 Pages/Partials/

Search.cshtml:


@model SearchModel

<body>
    <form method="get">
        <div class="d-flex flex-row">
            <div id="search-div" class="form-group">
                <input asp-for="@Model.SearchString" value="@Model.SearchString" class="form-control" id="search-bar" placeholder="Enter ID" />
            </div>
            <div>
                <button class="btn btn-primary" type="submit">Search</button>
            </div>
        </div>
    </form>
</body>

Search.chshtml.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace AdminPortal.Web.Pages.Partials
{
    public class SearchModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public string SearchString { get; set; }
                
        public void OnGet()
        {
        }
    }
}

Home.cshtml.cs:

@page "/Home"
@using AdminPortal.Web.Pages
@model HomeModel



<body>
    <partial name="Partials/Search" model="new Pages.Partials.SearchModel()" />
    <partial name="Partials/Map" model="new Pages.Partials.MapModel()" />
</body>

startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;

namespace AdminPortal.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

但是,当我点击提交时,它并没有调用OnGet()方法。我做错了什么?

您需要手动添加 action 值或通过 asp-page.

等表单标签辅助方法进行设置
<form method="get" action="/Search">
</form>

没有动作值,浏览器会将其发送到当前URL。当你有一个带有表单的页面并且它应该将它发送到相同的页面模型时,这很好用,比如 GET(显示页面)和 POST,发送表单。

但是,对于跨不同页面使用的部分,您需要通过设置正确的 URL.

来指定页面

However, when I click submit, it does not call the OnGet() method. What am I doing wrong?

那是因为partial view 是razor view 而不是razor pages.Razor view 只是一个view 和它一起工作 controller.More 详细解释你可以参考:

正确的做法是在首页添加后端代码:

Home.cshtml.cs:

public class HomeModel : PageModel
{
    [BindProperty(SupportsGet = true)]
    public string SearchString { get; set; }
    public void OnGet()
    {

    }
}

Search.cshtml:

@model HomeModel

<body>
    <form method="get">
          //...
    </form>
</body>

或者如果您不想在主页中使用默认的 OnGet 方法,您可以使用页面处理程序:

Home.cshtml:

@page "/Home/{handler?}"  //change here
@using AdminPortal.Web.Pages
@model HomeModel

<body>
    <partial name="Partials/Search" model="new Pages.Partials.SearchModel()" />
    <partial name="Partials/Map" model="new Pages.Partials.MapModel()" />
</body>

Home.cshtml.cs:

public class HomeModel : PageModel
{
    [BindProperty(SupportsGet = true)]
    public string SearchString { get; set; }
    public void OnGet()
    {

    }
    public void OnGetSearch()
    {
       //do your stuff...
    }
}

Search.cshtml:

@model HomeModel

<body>
    <form method="get" asp-page-handler="Search">
        //...
    </form>
</body>