由于托管提供商的限制,我被迫使用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
。有替代方法吗?
如@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" />