首页 > 解决方案 > 如何使用局部“获取”asp.net-core razor 页面中的内容?

问题描述

我有以下部分位于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()方法。我究竟做错了什么?

标签: c#asp.net-coregetpartials

解决方案


但是,当我单击提交时,它不会调用 OnGet() 方法。我究竟做错了什么?

那是因为部分视图是剃刀视图而不是剃刀页面。剃刀视图只是一个视图,它与控制器一起使用。更详细的解释您可以参考:

https://stackoverflow.com/a/50158395/11398810

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

主页.cshtml.cs:

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

    }
}

搜索.cshtml:

@model HomeModel

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

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

主页.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>

主页.cshtml.cs:

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

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

搜索.cshtml:

@model HomeModel

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

推荐阅读