MVC2 FormCollection 选项

发布于 2024-09-09 16:59:30 字数 204 浏览 5 评论 0原文

我刚刚开始使用 MVC2 并浏览 NerdDinner 示例。我注意到似乎有多种方法可以传递表单值,例如:

FormColelction formvalues
FormCollection collection
FormCollection form

为什么要使用一种而不是另一种,为什么? 这是否也与您是否使用实体框架有关?

I'm just getting started with MVC2 and going through the NerdDinner examples. I noticed that there seems to be multiple ways to pass in the form values for example:

FormColelction formvalues
FormCollection collection
FormCollection form

Why would you use one over the other and why?
Does it also relate to whether you are using Entity Framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤寂小茶 2024-09-16 16:59:30

上面的每个示例都传递相同的类型 (FormCollection),但变量名称不同。

如果 NerdDinner 能保持一定的一致性那就太好了。

您还可以让 MVC 自动从表单填充自定义对象,而不是使用通用 FormCollection。

EG 如果您发布的表单包含名字、姓氏和年龄(代表一个人)字段,您可以使用类似的方法

[HttpPost]
public ActionResult Create(Person person)
{
    //person is already populated    
}

Each of the above examples is passing in the same type (FormCollection) but with just a different variable name.

It would have been nice to see some consistency in NerdDinner.

You can also have MVC automagically populate a custom object from your form instead of using the generic FormCollection.

EG If your form being posted has fields for FirstName, LastName and Age (representing a person) you could have a method like

[HttpPost]
public ActionResult Create(Person person)
{
    //person is already populated    
}
泡沫很甜 2024-09-16 16:59:30
[HttpPost]
public ActionResult Create([Bind(Exclude = "id")]Person post_person)
{
      // post_person => auto populate formpost values 
      // [Bind(Exclude = "id")] => excluding auto populate identity field 
}
[HttpPost]
public ActionResult Create([Bind(Exclude = "id")]Person post_person)
{
      // post_person => auto populate formpost values 
      // [Bind(Exclude = "id")] => excluding auto populate identity field 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文