在asp.net核心剃须刀页面的部分视图中使用表单 [英] Using forms in partial views in asp.net core razor pages

查看:54
本文介绍了在asp.net核心剃须刀页面的部分视图中使用表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下结构:
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 .我在做什么错了?


I know, that there isn't any useful code in any of the methods, but I want the form to use the OnGet handlers in the code-behind file. It somehow keeps calling the ctor. What am I doing wrong?

推荐答案

提交表单时,如果要转到 https://localhost:xxx/Partials/SelectLoc?Location = xxx ,则您可以在表单中添加 action ="/Partials/SelectLoc" .

If you want to go to https://localhost:xxx/Partials/SelectLoc?Location=xxx when submit form,you can add action="/Partials/SelectLoc" in form.

SelectLoc.cshtml:

SelectLoc.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>

结果:

这篇关于在asp.net核心剃须刀页面的部分视图中使用表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆