【问题标题】:Using forms in partial views in asp.net core razor pages在 asp.net core razor 页面的部分视图中使用表单
【发布时间】: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


【解决方案1】:

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

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>
结果:

【讨论】:

  • 是的,我也没有添加@page。你能显示你使用局部视图的页面吗?为什么你在局部视图中有SelectLoc.cshtml.cs?
  • 为什么在局部视图中有 SelectLoc.cshtml.cs? 那是因为该模块将出现在每个页面上。所以,基本上在_Layout.cshtml
  • 另外,action=stuff 期望部分是一个页面
  • 如果你删除SelectLoc.cshtml的@page,SelectLocModel中的handlers将不起作用,因为它不是razor page。在这种情况下,你需要将mvc和razor page结合起来。让form在控制器中调用动作。
猜你喜欢
  • 2018-06-05
  • 2021-05-07
  • 2018-12-20
  • 2018-02-24
  • 2019-05-22
  • 2019-06-28
  • 2020-12-24
  • 2018-04-05
  • 2020-10-10
相关资源
最近更新 更多