首页 > 解决方案 > 等效于 ASP.NET Core 1.1 中的 BindPropertyAttribute

问题描述

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

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

在 ASP.NET Core 2.0 以后,我会使用BindPropertyAttribute如下:

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

但是,BindPropertyAttribute在 ASP.NET Core 1.1 中不受支持。有没有我可以使用的替代方案?

标签: c#asp.net-coreasp.net-core-1.1

解决方案


您可以使用ModelBinder属性:

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

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

<input name="g-recaptcha-response" class="form-control" />

推荐阅读