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

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

问题描述

我继承了用 ASP.Net MVC 4 编写的代码库.每个 post 方法都采用 FormCollection.除了必须通过带引号的字符串访问值的烦恼之外,它还导致一些缺点,例如无法使用诸如 ModelState.IsValid[AllowHtml] 属性之类的东西在我的 ViewModel 属性上.他们实际上确实为每个视图创建了 ViewModel 类(尽管它们几乎只是实际实体框架模型类的直接包装器),但它们仅用于 GET 方法.

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.

我对 FormCollection 有什么遗漏,说明为什么这实际上是一个好主意?它似乎只有缺点.我想通过使用 ViewModels 来完成并修复"它.这需要大量的工作,因为 ViewModel 的属性是接口而不是具体的类,这意味着要么编写自定义绑定器,要么更改 ViewModel.

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.

但也许我缺少一些使用 FormCollection 有意义的东西?

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

推荐答案

是否有充分的理由使用 FormCollection 而不是 ViewModel?

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

没有.我有以下问题.

问题 - 1

如果 FormCollection 正在被使用...将强制 Type Cast 不必要的 Primitive Type 值,因为在获取时System.Collections.Specialized.NameValueCollection 的特定 Index 的条目,返回的值为 String 类型.这种情况不会出现在强类型 View-Models 的情况下.

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.

问题 - 2

当您提交表单并转到Post Action Method,并且View-Model作为参数存在于Action方法中时,您可以提供发回Posted给你的价值View.否则,重新编写代码通过TempData/ViewData/ViewBag

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 是普通的类,创建用于绑定数据到视图

问题 - 3

我们有可以在 View ModelCustom Validations 中实现的数据注释.

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

ASP.Net MVC 使用数据注释简化模型验证.数据注释是应用于属性的属性.我们可以通过继承内置的验证属性类来创建自定义验证属性.



问题 - 4

例如您有以下 HTML

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

问题:我们如何从上面(例如从控制器内部)访问 customAttr1 的值

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.

替代方案:使用一点 jQuery 来获取自定义属性值,并将其与表单值一起发布到操作方法

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




这就是原因,我总是更喜欢使用 View-Models

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

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