微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用部分来“获取”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() 方法。我做错了什么?

解决方法

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

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

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

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

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>
,

您需要手动添加 action 值或通过诸如 asp-page 之类的表单标签助手方法设置它。

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。