Reputation: 2238
I have the following partial located at 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();
});
}
}
}
However, when I click submit, it does not call the OnGet()
method. What am I doing wrong?
Upvotes: 0
Views: 1608
Reputation: 36715
However, when I click submit, it does not call the OnGet() method. 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:
https://stackoverflow.com/a/50158395/11398810
The correct way is to add the backend code in your Home page:
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>
Or if you do not want to use the default OnGet method in Home page,you could use page handler:
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>
Upvotes: 1
Reputation: 2601
You need to add the action
value manually or set it via form tag helper methods like asp-page
.
<form method="get" action="/Search">
</form>
Without an action value, the browser will send it to the current 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.
However, with your partial - that is used across different pages - you need to specify the page by setting the correct URL.
Upvotes: 0