有没有什么好的理由使用的FormCollection而不是视图模型? [英] Is there any good reason to use FormCollection instead of ViewModel?

查看:145
本文介绍了有没有什么好的理由使用的FormCollection而不是视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承写在ASP.Net MVC 4. code基每一个岗位的方法需要一个的FormCollection 。除了有通过带引号的字符串来访问值的烦恼,这也导致了诸如不能够使用像 ModelState.IsValid 的东西,或 [AllowHtml] 属性在我的ViewModel属性。他们实际上并创建视图模型类的每一个意见,(尽管它们是pretty实际的实体框架模型类围绕简单,只是直接包装),但它们仅用于GET方法。

有什么我缺少有关的FormCollection,给出了一个原因,这可能实际上是一个好主意吗?它似乎只有缺点。我想用的ViewModels,而不是去通过和修​​复了。这将需要工作的一个好一点的,因为具有的ViewModels是接口的性质,而不是具体的类,这意味着要么编写自定义粘合剂或改变的ViewModels。

但也许还有什么我失踪的地方是有意义使用的FormCollection?


解决方案

  

有没有什么好的理由使用的FormCollection而不是视图模型?


没有。我有以下的问题。

期 - 1

在案件的FormCollection 正在使用......这将是强制性的类型转换原始类型值未必然,因为在获取 System.Collections.Specialized.NameValueCollection ,返回值的特定指数的项类型字符串。这种情况将不区分进来的强类型查看 - 型号

期 - 2

当您提交表单并转到发表操作方法和视图模型作为参数的存在操作方法,你必须提供给发布值发送给您查看。否则,再写code通过的TempData /的ViewData / ViewBag

发回

查看 - 模特们正常上课,创建数据绑定到从视图
搜索结果

期 - 3

我们有数据注释,可以在实现视图模型自定义验证

ASP.Net MVC简化了使用数据注释模型validatons。数据注释是thyat被应用在性能属性。我们可以通过继承内置的验证属性类中创建自定义的验证属性。

搜索结果

期 - 4

例如你有以下的 HTML

 <输入类型=文本名称=textBox1的VALUE =戒customAttr1 =myvalue的/>

:我们怎样才能从上面如从访问控制器内customAttr1值

:当一个表单才能发布,只有名称和元素的值回发到服务器

替代:使用jQuery的一点,以获得自定义属性值,和后期,随着表单值的操作方法。

另一个选择是宁愿把你得到了什么在自定义的隐藏控件属性

结果搜索结果
这就是原因,我总是preFER使用查看 - 型号

I've inherited a code base written in ASP.Net MVC 4. Every post method takes a FormCollection. Aside from annoyance of having to access the values through quoted strings, it also leads to drawbacks such as not being able to use things like ModelState.IsValid, or [AllowHtml] attributes on my ViewModel properties. They actually did create ViewModel classes for each of their views, (though they are pretty much just direct wrappers around the actual Entity Framework Model classes), but they are only used for the GET methods.

Is there anything I'm missing about FormCollection that gives a reason why this may have actually been a good idea? It seems to only have drawbacks. I'd like to go through and "fix" it by using ViewModels instead. This would take a good bit of work because the ViewModels have properties that are interfaces and not concrete classes, which means either writing a custom binder or changing the ViewModels.

But perhaps there's something I'm missing where it makes sense to use FormCollection?

解决方案

Is there any good reason to use FormCollection instead of ViewModel?

No. I have following issues.

Issue - 1

In case FormCollection is being used...It will be mandatory to Type Cast the Primitive Type Values un-necessarily because while getting the entry of specific Index of the System.Collections.Specialized.NameValueCollection, value being returned is of type String. This situation will not come in case of Strongly Typed View-Models.

Issue - 2

When you submit the form and goes to Post Action Method, and View-Model as Parameter exists in the Action method, you have the provision to send back the Posted Values to you View. Otherwise, write the code again to send back via TempData/ViewData/ViewBag

View-Models are normal classes, created to bind data to-from Views

Issue - 3

We have Data Annotations that can be implemented in View Model or Custom Validations.

ASP.Net MVC simplifies model validatons using Data Annotation. Data Annotations are attributes thyat are applied over properties. We can create custom validation Attribute by inheriting the built-in Validation Attribute class.



Issue - 4

Example you have the following HTML

<input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" />

Question : How can we access the value of customAttr1 from the above eg from inside the controller

Answer : When a form get posted only the name and value of elements are posted back to the server.

Alternatives : Use a bit of jQuery to get the custom attribute values, and post that along with the form values to action method

Another option is to rather put what you got in your custom attributes in hidden controls




That's the reason, I would always prefer to use View-Models

这篇关于有没有什么好的理由使用的FormCollection而不是视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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