如何在 razor 页面中使用 PageRemote
how to use PageRemote in razor pages
我的代码如下:
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 核心有关。
Url 生成参数的名称取决于您输入的名称。
为什么你的 url 会像 https://localhost:44377/Libs/Create?handler=IsKeyExists&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" />
我的代码如下:
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 核心有关。
Url 生成参数的名称取决于您输入的名称。
为什么你的 url 会像 https://localhost:44377/Libs/Create?handler=IsKeyExists&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" />