简体   繁体   English

将模型绑定HTML控件绑定到具有不同名称的模型属性

[英]Model Binding HTML control to Model Property with a different name

I have an HTML input control on one of my views that is called for example 'BodyInput'. 我在其中一个视图上有一个HTML输入控件,例如“ BodyInput”。 When I post the form I am checking if the ModelState is valid or not. 发布表单时,我正在检查ModelState是否有效。 The other few input controls on my view validate fine, as the name of the properties in my model are exactly the same as the name of the HTML controls, but the name of my property for the 'BodyInput' control to map to, is 'Body'. 我的视图上的其他几个输入控件可以很好地验证,因为模型中的属性名称与HTML控件的名称完全相同,但是要映射到“ BodyInput”控件的属性的名称为“身体'。

Lets say I can't change the name of either the property in my model, or the Html input control - can I use data annotations (or anything) to map/associate the property and control for model binding? 可以说我无法更改模型中属性的名称或HTML输入控件的名称-我可以使用数据注释(或其他任何方式)来映射/关联属性和控件以进行模型绑定吗?

Found a solution in another question as pointed out by user ipr101. 在用户ipr101指出的另一个问题中找到了解决方案。

Bind formValue to property of different name, ASP.NET MVC 将formValue绑定到其他名称的属性ASP.NET MVC

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

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