简体   繁体   English

如何使用部分“获取”asp.net-core razor 页面中的内容?

[英]How to use partials to “get” stuff in asp.net-core razor pages?

I have the following partial located at Pages/Partials/我有以下部分位于Pages/Partials/

Search.cshtml : 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 : 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 : 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 : 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();
            });
        }
    }
}

However, when I click submit, it does not call the OnGet() method.但是,当我单击提交时,它不会调用OnGet()方法。 What am I doing wrong?我究竟做错了什么?

However, when I click submit, it does not call the OnGet() method.但是,当我单击提交时,它不会调用 OnGet() 方法。 What am I doing wrong?我究竟做错了什么?

That is because partial view is a razor view instead of a razor pages.Razor view is just a view and it works with controller.More detailed explanation you could refer to: That is because partial view is a razor view instead of a razor pages.Razor view is just a view and it works with controller.More detailed explanation you could refer to:

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

The correct way is to add the backend code in your Home page:正确的方法是在你的首页添加后端代码:

Home.cshtml.cs:主页.cshtml.cs:

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

    }
}

Search.cshtml:搜索.cshtml:

@model HomeModel

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

Or if you do not want to use the default OnGet method in Home page,you could use page handler:或者,如果您不想在主页中使用默认 OnGet 方法,您可以使用页面处理程序:

Home.cshtml:主页.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:主页.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:搜索.cshtml:

@model HomeModel

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

You need to add the action value manually or set it via form tag helper methods like asp-page .您需要手动添加action值或通过表单标签辅助方法(如asp-page )设置它。

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

Without an action value, the browser will send it to the current URL.如果没有操作值,浏览器会将其发送到当前的 URL。 This works fine when you have a page with a form and it should send it to the same page model, like GET (display the page) and POST, send the form.当您有一个带有表单的页面并且它应该将其发送到同一页面 model 时,这可以正常工作,例如 GET(显示页面)和 POST,发送表单。

However, with your partial - that is used across different pages - you need to specify the page by setting the correct URL.但是,对于跨不同页面使用的部分 - 您需要通过设置正确的 URL 来指定页面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM