【问题标题】:how to use PageRemote in razor pages如何在剃须刀页面中使用 PageRemote
【发布时间】:2020-04-15 06:02:02
【问题描述】:

我的代码如下:

public class Lib
{
    public int ID { get; set; }

    [Required]
    [PageRemote(PageHandler = "IsKeyExists", HttpMethod = "Get")]
    public string Key { get; set; }
  }

在创建页面模型中:

    public async Task<IActionResult> OnGetIsKeyExistsAsync(string key)
    {
        var query = _context.Lib.Any(l => l.Key == key);
        if (query)
        {
            return new JsonResult($"Key {key} exists");
        }

        return new JsonResult(true);
    }

当我调试代码时,OnGetIsKeyExistsAsync 总是获取值为 null 的参数键。

我发现 browe 中的请求是:

https://localhost:44377/Libs/Create?handler=IsKeyExists&Lib.Key=xx

我用PostMan测试,用Key修改参数名,一切正常。

https://localhost:44377/Libs/Create?handler=IsKeyExists&Key=xx

我不想修改我的页面模型来绑定另一个字符串值,以及如何使它与 Lib.Key 一起工作?

也许这个问题与远程页面无关,仅与 razor 页面或 asp.net 核心有关。

【问题讨论】:

  • 您的剃须刀页面浏览量如何?

标签: asp.net-core-mvc razor-pages


【解决方案1】:

Url 生成参数的名字取决于你输入的名字。

你的url会生成https://localhost:44377/Libs/Create?handler=IsKeyExists&amp;Lib.Key=xx这样的url的原因是asp-for默认会生成名字:

<input asp-for="Lib.Key"  />

生成html:

<input type="text" id="Lib_Key" name="Lib.Key" data-val="true" data-val-remote="'Key' is invalid." data-val-remote-additionalfields="*.Key" data-val-remote-type="Get" data-val-remote-url="/?handler=IsKeyExists" data-val-required="The Key field is required." value="">

如果您想使用Lib.Key,您在处理程序中收到的参数应该是如下对象:

public async Task<IActionResult> OnGetIsKeyExistsAsync(Lib Lib)

如果您不想更改处理程序,则需要指定如下名称:

<input asp-for="Lib.Key" name="key" />

【讨论】:

  • 非常感谢!我修改了 OnGetIsKeyExistsAsync(Lib lib) 函数,效果很好。
猜你喜欢
  • 1970-01-01
  • 2021-10-22
  • 2020-11-11
  • 2020-11-06
  • 2021-10-27
  • 2020-09-21
  • 2021-10-11
  • 1970-01-01
  • 2019-03-13
相关资源
最近更新 更多