【问题标题】:Is there a way to access a complex model property of a page handler in the Razor page?有没有办法访问 Razor 页面中页面处理程序的复杂模型属性?
【发布时间】:2022-09-23 00:45:03
【问题描述】:

我有一个页面处理程序,它具有用于模型绑定的复杂属性。我想访问剃刀页面的属性,以便我可以使用标签助手从属性生成表单输入。

这是我的页面处理程序:

public async Task<IActionResult> OnPostChangePassword(PasswordChangeInput Input)
{
     // Some code

}

我想访问该属性,以便可以将其与asp-for 一起使用

@page
@model EditPasswordModel
@
{
    //Some code
}

<form method=\"post\">
    <div class=\"flex justify-between my-1\">
        <label class=\"text-gray-700\" for=\"cus_name\">Password:</label>
        <input asp-for=\"Input.Password\" class=\"px-3 py-1 text-gray-900\" />
    </div>
    <div class=\"flex justify-between my-1\">
        <label class=\"text-gray-700\" for=\"cus_name\">Confirm Password:</label>
        <input asp-for=\"Input.ConfirmPassword\" class=\"px-3 py-1 text-gray-900\" />
    </div>
    <div>
        <input type=\"submit\" />
    </div>
</form>
  • 我正在使用 ASP.NET Core Razor 页面

标签: c# asp.net asp.net-core razor razor-pages


【解决方案1】:

在您的 PageModel 类中,您需要添加具有 [BindProperty] 属性的 PasswordChangeInput 属性。

public class EditPasswordModel : PageModel
{
    [BindProperty]
    public PasswordChangeInput Input { get; set; }

    ...
}

然后,您可以将表单绑定到此属性,就像您在问题中显示的那样。

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 2022-01-10
    • 2018-10-31
    • 2020-04-20
    • 2021-11-24
    • 2022-08-23
    • 2019-08-02
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多