【发布时间】:2021-05-06 11:28:26
【问题描述】:
我的结构如下:SelectLoc.cshtml:
@model SelectLocModel
<div class="dropdown">
<form method="get">
<select asp-for="Location" asp-items="Model.Locations"
class="btn btn-secondary" formaction="Partials/SelectLoc" onchange="this.form.submit()">
</select>
</form>
</div>
SelectLoc.cshtml.cs:
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
namespace AdminPortal.Web.Pages.Partials
{
public class SelectLocModel : PageModel
{
private readonly HttpClient httpClient;
private readonly string key = "FAKE TOKEN";
public string Location { get; set; }
public List<SelectListItem> Locations { get; } = new List<SelectListItem>
{
new SelectListItem { Value = null, Text = "Select Location" },
new SelectListItem { Value = "Kothrud", Text = "Kothrud" },
new SelectListItem { Value = "Dhanakawdi", Text = "Dhanakawdi" },
new SelectListItem { Value = "Karvenagar", Text = "Karvenagar" },
new SelectListItem { Value = "Wakad", Text = "Wakad" },
};
public SelectLocModel(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public void OnSubmit()
{
}
public void OnGet()
{
}
public void OnGetSubmit()
{
}
public async void OnGetLocation()
{
string geocodeRequest = $"https://maps.googleapis.com/maps/api/geocode/json?address={Location}&key={key}";
Location jsonResponse = await httpClient.GetFromJsonAsync<Location>(geocodeRequest);
}
}
}
我知道,任何方法中都没有任何有用的代码,但我希望表单使用代码隐藏文件中的 OnGet 处理程序。它以某种方式一直调用ctor。我做错了什么?
【问题讨论】:
-
你的意思是你在其他页面中使用该页面作为部分页面?并且提交表单时,它不会转到
https://localhost:xxx/SelectLoc?Location=xxx。
标签: c# asp.net-core get partial-views razor-pages