asp.net mvc的强类型的辅助 - 应该渲染绑定对象是一样的发布对象吗? [英] asp.net mvc strongly typed helpers - should your render binding object be the same as your posting object?

查看:186
本文介绍了asp.net mvc的强类型的辅助 - 应该渲染绑定对象是一样的发布对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到asp.net MVC 2已经强类型的帮助,最初看着它的工作原理我想,也许我在数据绑定渲染视图方面做一些错误的asp.net的MVC 1和后回的方式控制器。

i see that asp.net mvc 2 has strongly typed helped and looking initially at the way it works i think maybe i am doing something wrong in asp.net mvc 1 in terms of data binding to render the view and post back to the controller.

所以经常用于呈现视图和回发到控制器不同的对象。这是错的?似乎天生为渲染视图,当你经常有有下拉菜单等,但你发布你只想所需要回发的属性列表的视图模型。

I often have different objects for rendering the view and posting back to the controller. is this wrong ?? It seems natural as when rendering the view you often have a viewmodel that has lists for dropdowns, etc. but for your posting you only want the properties that are needed to post back.

例如,在为渲染的方式,我的视图模型可能看起来像这样

for example, on the way in for rendering, my viewmodel might look like this

 public class PersonViewModel
 {
      public int Age;
      public string FIrst;
      public JobCategory[] JobCategories;
      public Sport[] Sports;
      public int NumberOfChildren;

 }

在这种情况下, jobCategories 体育将被用于填充一个下拉框。 NumberOfchildren 将会只是HTML放的,我不希望它编辑。当我想张贴我只是想传递回只用贴属性苗条对象,所以我有另一个对象

in this case, jobCategories and Sports is going to be used to populate a dropdown box. NumberOfchildren is going to just be html put in and i dont want it editable. When i want to post i only want to pass back a slim object with just the posted properties so i have another object

  public class PersonUpdater
 {
      public int Age;
      public string FIrst;
      public int JobCategoryId;
 }

这些都是我需要回传,所以我的控制器看起来像这样唯一的属性:

these are the only properties that i need to pass back so my controller will look like this:

 public ActionResult Update(PersonUpdater personUpdater)
 {
      _repository.UpdateModel(personUpdater). 
 }

鉴于上述情况,假设强类型的辅助方法(下)

因此​​,似乎在这样有用,但随后可能上发布回服务器,如果你referrring到不同的属性引起的问题。

so, given the above, assuming the strongly typed helper methods (below) seem useful for the way in but then may cause issues on posting back to the server if you are referrring to different properties.

<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx

什么想法?

推荐答案

真正的问题是 - 目前接受的方法忽略对于视图模型SRP 一点 - 编辑表单作为输入和输出的同时

Real problem is - current accepted approach ignores SRP for view models a bit - edit form acts as input and output simultaneously.

人们还没有接受尚未划分视图模型成,因为我给他们打电话,输入视图模型输出视图模型(许多 - 甚至创建视图模型层实在是太多了)。因此 - MVC2目前缺乏这种支持(你不应该有强类型的观点,这不是输入和输出的同时),主要是因为普遍接受的做法模糊和缺乏

People haven't accepted yet dividing view model into, as i call them, input view model and output view model (for many - even creating view model layer is too much). Therefore - Mvc2 currently lacks support for this (you aren't supposed to have strongly typed view that's not input and output at the same time) mainly because of vagueness and lack of broadly accepted approaches.

但我认为,有一个增益(好吧......它实际上是一个权衡)的不断深入和分离视图模型放进去的2。我不会感到惊讶,如果这个想法会进化,最终成为被广泛接受的。

But i do think that there's a gain (ok... it's actually a trade off) in going deeper and separating view model into 2 of them. And i won't be surprised if this idea will evolve and eventually become widely accepted.

其实 - 目前的做法,甚至有一个名字 - <一个href=\"http://$c$cbetter.com/blogs/jeremy.miller/archive/2008/10/23/our-opinions-on-the-asp-net-mvc-introducing-the-thunderdome-principle.aspx\"相对=nofollow> Thunderdome原则。而如果像杰里米·米勒D.家伙说,这是正确的,社会不会理会,也不会寻求别的。

Actually - current approach even has a name - Thunderdome principle. And if guys like Jeremy D. Miller says this is correct, community won't bother and won't search for anything else.

从实际的角度来看 - 一些可以通过提供正确的元数据缓解问题(你可能想看看<一个href=\"http://weblogs.asp.net/rashid/archive/2009/12/24/introducing-fluent-metadataprovider-for-asp-net-mvc.aspx\"相对=nofollow>流畅模型元数据提供商)。

From practical point of view - some of the issues you can mitigate through providing correct metadata (you might want to check out fluent model metadata provider).

这篇关于asp.net mvc的强类型的辅助 - 应该渲染绑定对象是一样的发布对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆