相当于ASP.NET Core 1.1中的BindPropertyAttribute

问题描述 投票:0回答:1

由于托管提供商的限制,我被迫使用ASP.NET Core 1.1。

我创建了一个模型类,希望该模型绑定到与模型属性名称不同的命名值。

在ASP.NET Core 2.0及更高版本中,我将按以下方式使用BindPropertyAttribute

public class MyModel
{
    [BindProperty(Name="g-recaptcha-response")]
    public string GoogleReCaptchaResponse { get; set; }
}

但是,ASP.NET Core 1.1中不支持BindPropertyAttribute。有替代方法吗?

c# asp.net-core asp.net-core-1.1
1个回答
0
投票

如@Kirk Larkin所建议,您可以使用ModelBinder属性:

[ModelBinder(Name = "g-recaptcha-response")]
public string GoogleReCaptchaResponse { get; set; }

在视图中,应将元素name设置为g-recaptcha-response以进行模型绑定:

<input name="g-recaptcha-response" class="form-control" />
© www.soinside.com 2019 - 2024. All rights reserved.