Equivalent to BindPropertyAttribute in ASP.NET Core 1.1

260 Views Asked by At

Due to limitations imposed by a hosting provider, I am forced to use 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.

In ASP.NET Core 2.0 onward, I would use the BindPropertyAttribute as follows:

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. Is there an alternative I can use instead?

1

There are 1 best solutions below

0
Nan Yu On BEST ANSWER

You can use ModelBinder attribute :

[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:

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