有没有什么好的理由使用的FormCollection而不是视图模型? [英] Is there any good reason to use FormCollection instead of ViewModel?
问题描述
我继承写在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 likeModelState.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 toType Cast
thePrimitive Type
Values un-necessarily because while getting the entry of specific Index of theSystem.Collections.Specialized.NameValueCollection
, value being returned is of typeString
. This situation will not come in case of Strongly TypedView-Models
.Issue - 2
When you submit the form and goes to
Post
Action Method, andView-Model
as Parameter exists in the Action method, you have the provision to send back the Posted Values to youView
. Otherwise, write the code again to send back viaTempData/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
orCustom 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 useView-Models
这篇关于有没有什么好的理由使用的FormCollection而不是视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!