【问题标题】:How to use partials to "get" stuff in asp.net-core razor pages?如何使用局部“获取”asp.net-core razor 页面中的内容?
【发布时间】:2021-01-19 09:24:14
【问题描述】:

我有以下部分位于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="/Partials/Search" 可能是一个解决方案。但实际上如果您不指定动作,它在我的项目中也可以正常工作。我认为您需要找出导致无法正常工作的原因在您的项目中。请分享您的 Startup.cs。并请在首次渲染搜索页面时检查浏览器中的请求url是否为/Partials/Search
  • 嗨@d4rk4ng31,你可以看到你在首页添加了Partials/Search作为部分,请求的url应该是/Home。当你提交表单时,它会转到/Home,这就是为什么您没有进入搜索 OnGet 方法的原因。在您的场景中,您必须指定表单操作:action="/Partials/Search"
  • 您介意分享您的主页后端代码吗?您的主页在哪里?在 Pages 文件夹或任何其他文件夹中?
  • @Rena。我在chat room

标签: c# asp.net-core get partials


【解决方案1】:

但是,当我单击提交时,它不会调用 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 方法,您可以使用页面处理程序:

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>

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

【讨论】:

  • 我所要做的就是使用formaction 属性。
  • 正如我的第一条评论所说,手动添加formaction是一种解决方案。但你需要知道为什么你会犯这样的错误。因为你第一次改变formaction,你说它给你404.404意味着找不到页面。这就是为什么我昨天和你咨询了很多。
  • 嗯,但是为什么它不适用于action 属性呢?
  • 你是说asp-action?
【解决方案2】:

您需要手动添加action 值或通过asp-page 等表单标签辅助方法设置它。

<form method="get" action="/Search">
</form>

如果没有操作值,浏览器会将其发送到当前 URL。当您有一个带有表单的页面并且它应该将其发送到相同的页面模型时,这可以正常工作,例如 GET(显示页面)和 POST,发送表单。

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

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 2022-01-19
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2019-09-21
    • 2018-06-05
    相关资源
    最近更新 更多