两个名称相同的字段

编程入门 行业动态 更新时间:2024-10-28 02:30:35
本文介绍了两个名称相同的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ViewModel类来封装个人"和业务"模型.我的问题是,两个模型都有一个名为"Email"的属性,并且模型绑定无法区分两者.

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.

我读到[Bind(Prefix = ...用于解决此问题,但我无法看到有关如何实现此目的的简洁示例.

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?

推荐答案

我相信,如果发布的表单元素的名称中包含前缀,则绑定将正确完成.这就是模板化助手(即EditorFor)呈现控件的方式,并且我的嵌套viewmodel被正确绑定.例如,在您的情况下,您的视图将具有如下所示的表单元素:

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:

... <input type="text" name="Business.Email" value="<%=this.Model.Business.Email %>" /> ... <input type="text" name="ContactPerson.Email" value="<%=this.Model.ContactPerson.Email %>" /> ...

或者,使用模板化助手(在mvc 2中):

Or, using templated helpers (in mvc 2):

... <%= Html.TextBoxFor(m => m.Business.Email) %> ... <%= Html.TextBoxFor(m => m.ContactPerson.Email) %> ...

您的控制器只需将BusinessFormViewModel作为参数即可,

And your controller would simply take a BusinessFormViewModel as a parameter, as so:

public BusinessFromView(BusinessFormViewModel businessForm) { Business = businessForm.Business; ContactPerson = businessForm.ContactPerson; }

更多推荐

两个名称相同的字段

本文发布于:2023-10-26 17:06:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   名称   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!