简体   繁体   English

等效于 ASP.NET Core 1.1 中的 BindPropertyAttribute

[英]Equivalent to BindPropertyAttribute in ASP.NET Core 1.1

Due to limitations imposed by a hosting provider, I am forced to use ASP.NET Core 1.1.由于托管服务提供商的限制,我被迫使用 ASP.NET Core 1.1。

I have created a model class and wish to have that model bind to a named value that is different to the name of the model property.我创建了一个 model class 并希望将 model 绑定到与 model 绑定到与 model 属性不同的命名值。

In ASP.NET Core 2.0 onward, I would use the BindPropertyAttribute as follows:在 ASP.NET Core 2.0 以后,我将使用BindPropertyAttribute ,如下所示:

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

However, the BindPropertyAttribute is not supported in ASP.NET Core 1.1.但是, BindPropertyAttribute Core 1.1 不支持 BindPropertyAttribute。 Is there an alternative I can use instead?有没有我可以使用的替代方案?

You can use ModelBinder attribute:您可以使用ModelBinder属性:

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

In the view, you should set the element name to g-recaptcha-response for model binding:在视图中,您应该将元素name设置为g-recaptcha-response以进行 model 绑定:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM