c# - 等效于 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 中不受支持。有没有我可以使用的替代方案?
解决方案
您可以使用ModelBinder
属性:
[ModelBinder(Name = "g-recaptcha-response")]
public string GoogleReCaptchaResponse { get; set; }
在视图中,您应该将元素name
设置g-recaptcha-response
为模型绑定:
<input name="g-recaptcha-response" class="form-control" />
推荐阅读
- security - SSH:自签名 - 无法建立主机时的真实性
- gcc - GCC BareMetal 链接器问题 - Cortex M3
- dependency-injection - 如何从 IServiceProvider.GetServices 按基类型获取实例
(aspnet核心DI)? - html - 无法从 Z 轴翻转 SVG 圆
- javascript - 调用 firebase 函数返回 null
- python - 字符串值未显示在使用 matplotlib-python 创建的图形上
- python - 按子值对字典进行排序
- ios - 尝试使用 Swift HealthKit 会导致 Apple WatchKit 在视图加载时崩溃?
- module - Rust 参考模块
- bash - Bash 在 || 之后执行多个命令 如果非 0