簡體   English   中英

在 asp.net 核心 razor 頁面的部分視圖中使用 forms

[英]Using forms in partial views in asp.net core razor pages

我有以下結構:
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 我究竟做錯了什么?

如果您想在提交表單時將 go 到https://localhost:xxx/Partials/SelectLoc?Location=xxx ,您可以在表單中添加action="/Partials/SelectLoc"

選擇Loc.cshtml:

@model SelectLocModel
<div class="dropdown">
<form method="get" action="/Partials/SelectLoc">
    <select asp-for="Location" asp-items="Model.Locations"
            class="btn btn-secondary" formaction="Partials/SelectLoc" onchange="this.form.submit()">
    </select>
</form>
結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM