两个同名字段
我有一个 ViewModel 类来封装“个人”和“业务”模型。我的问题是,两个模型都有一个名为“电子邮件”的属性,并且模型绑定无法区分两者。
我读到 [Bind(Prefix = ...
) 用于解决此问题,但我还没有看到有关如何实现此目的的简明示例。
public class BusinessFormViewModel
{
public Business Business { get; set; }
public ContactPerson ContactPerson { get; set; }
public BusinessFromView(Business business, ContactPerson contactPerson)
{
Business = business;
ContactPerson = contactPerson;
}
}
如何使用 Bind Prefix 来解决 此问题解决这个问题吗?
I have a ViewModel class to encapsulate "Personal" and "Business" models. My problem is that both models have a property called "Email" and the model binding is not able to make a distinction between the two.
I read that [Bind(Prefix = ...
is used to resolved this issue, but I have not been able to see a concise example on how to achieve this.
public class BusinessFormViewModel
{
public Business Business { get; set; }
public ContactPerson ContactPerson { get; set; }
public BusinessFromView(Business business, ContactPerson contactPerson)
{
Business = business;
ContactPerson = contactPerson;
}
}
How do I use the Bind Prefix to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信,如果发布的表单元素的名称中包含前缀,则绑定将正确完成。这就是模板化助手(即 EditorFor)呈现控件的方式,并且我的嵌套视图模型已正确绑定。例如,在您的情况下,您的视图将具有如下所示的表单元素:
或者,使用模板化助手(在 mvc 2 中):
并且您的控制器将简单地将 BusinessFormViewModel 作为参数,如下所示:
I believe that if the form elements that are posted have prefixes included in the name, the binding will be done properly. This is how the templated helpers (i.e. EditorFor) renders the controls, and my nested viewmodels are bound properly. For example, in your case, your view would have form elements something like this:
Or, using templated helpers (in mvc 2):
And your controller would simply take a BusinessFormViewModel as a parameter, as so: