是否有一种标准方法可以为在 ASP.NET MVC2 中使用集合的对象实现编辑表单?
给定一个仅包含字段的极其简单的对象,例如:
class Contact {
string firstName;
string lastName;
DateTime birthday;
...
}
当您添加具有视图内容“创建”的强类型视图时,您会得到一个漂亮的表单,其中包含对象的所有字段,该表单将表单传递回控制器等,并且生活是美好的。
然而,当对象变得稍微复杂一点时,比如我们想要存储联系人的电子邮件地址(当然,一个联系人肯定可以有多个电子邮件地址):
class Contact {
string firstName;
string lastName;
DateTime birthday;
ICollection<EmailAddress> emailAddresses;
...
}
现在,当您添加带有视图内容“创建”的强类型视图时”,您将得到与以前相同的表单,并且集合不会以任何方式在表单中表示。
现在您有了一个完整的表单,但有一个例外:您希望添加一个部分,用户可以在其中输入任意数量的电子邮件地址,并将这些地址打包并在提交时传递给控制器。
在 ASP.NET MVC2 中有执行此操作的标准最佳实践方法吗?如果是的话,那是什么?
Given an extremely simple object with only fields, e.g.:
class Contact {
string firstName;
string lastName;
DateTime birthday;
...
}
When you add a strongly-typed View with view content "Create", you get a nice form with all the fields of your object that passes the form back to the controller, etc, and life is good.
However, when the object becomes slightly more complex, like say we want to store email addresses for Contacts (and of course a Contact can certainly have more than one email address):
class Contact {
string firstName;
string lastName;
DateTime birthday;
ICollection<EmailAddress> emailAddresses;
...
}
Now when you add the strongly-typed view with view Content "Create", you get the same form as before, and the collection is not represented in the form in any way.
So now you have a form which is complete with one exception: you would like to add a section where the user can enter in as many or as few email addresses as they like and have those wrapped up and passed to the Controller on submit.
Is there a standard best-practice way of doing this in ASP.NET MVC2? If so, what is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVC3 可以更好地处理这个问题,但是当您使用 MVC2 时,请查看 Steve Sanderson 有关编辑可变长度列表(ASP.NET MVC 2-style)的详细帖子。
Phil Haack 的模型绑定到列表还为您提供了有关默认 MVC2 模型绑定程序如何处理列表的更多信息
MVC3 handles this a lot better but as your using MVC2 take a look at Steve Sanderson's detailed post on Editing a variable length list, ASP.NET MVC 2-style.
Phil Haack's Model binding to a list also gives you further information on how the default MVC2 model binders handle lists
您可以拥有整个视图
好吧,可以想象,如果您按照我发布到 如何显示 asp.net 缓存的内容? 以允许编辑器深入研究您的模型。
这不是最佳解决方案。在 MVC 中操作列表数据非常困难。主要是因为多年的 ASP.NET 开发使我们与 Web 的金属如此脱节,以至于我们很容易忘记编辑列表客户端的概念。
为了使用列表,您很可能还需要使用客户端模板,以便能够轻松添加新元素和删除元素。这可能会变得非常复杂,因为 MVC 要求对所有列表进行索引并按数字进行跟踪,否则在回发时您将丢失项目(旁白:我觉得这是一个糟糕的设计决策)
现在话虽这么说,我建议查看 KnockoutJS 和 KnockoutMapping 框架结合 jQuery 和 jQuery 模板将允许您创建一个非常丰富的客户端经验。不幸的是,这很可能与您现有的开发风格截然不同,但我认为 Knockout 带来的是革命性的,并将进一步向 ASP.NET MVC 开发人员开放 Web。
Well, conceivably you can have your entire view be
If you follow the answer I posted to How to display the content of asp.net cache? to allow the Editor to do a deep dive against your model.
That's not the optimal solution. Manipulating List data in MVC is HARD. Mostly because of years of ASP.NET development has left us so disjoint from the metal of the web that the concepts of editing a list client side is easily lost on us.
For working with lists, you will also most likely need to work with client templating to be able to add new elements and remove elements easily. This can be get very complex due to the fact MVC requires all lists to be indexed and follow numerically otherwise on post backs you will be missing items (aside: I feel this was a terrible design decision)
Now with this being said, I would recommend looking at the KnockoutJS and KnockoutMapping frameworks which with the combination of jQuery and jQuery templating will allow you to create a very rich client experience. This unfortunately will most likely be a very radical departure from your existing development style however I feel what Knockout brings to the table is revolutionary and will open up the web so much further to ASP.NET MVC developers.