在 ASP.NET MVC2 创建方法中使用 FormCollection 的正确方法?
我目前正在使用新的 ASP.NET MVC2 框架开发一个应用程序。最初我开始在 ASP.NET MVC1 中编写这个应用程序,基本上只是将其更新到 MVC2。
我的问题是,我并没有真正理解 FormCollection 对象与旧 Typed 对象的概念。
这是我当前的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
Member member = new Member();
member.FirstName = collection["FirstName"];
member.LastName = collection["LastName"];
member.Address = collection["Address"];
// ...
return RedirectToAction("Details", new { id = member.id });
}
catch
{
return View("Error");
}
}
这是来自 MVC1 应用程序的代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Member member)
{
try
{
memberRepository.Add(member);
memberRepository.Save();
return RedirectToAction("Details", new { id = member.id });
}
catch
{
}
return View(new MemberFormViewModel(member, memberRepository));
}
在 MVC2 中切换到 FormCollection 有什么好处,更重要的是 - 如何正确使用它?
I am currently developing an application with the new ASP.NET MVC2 framework. Originally I started writing this application in the ASP.NET MVC1 and I'm basically just updating it to MVC2.
My problem here is, that I don't really get the concept of the FormCollection object vs. the old Typed object.
This is my current code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
try
{
Member member = new Member();
member.FirstName = collection["FirstName"];
member.LastName = collection["LastName"];
member.Address = collection["Address"];
// ...
return RedirectToAction("Details", new { id = member.id });
}
catch
{
return View("Error");
}
}
This is the Code from the MVC1 application:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Member member)
{
try
{
memberRepository.Add(member);
memberRepository.Save();
return RedirectToAction("Details", new { id = member.id });
}
catch
{
}
return View(new MemberFormViewModel(member, memberRepository));
}
What are the benefits of switching to FormCollection in MVC2 and more importantly - how is it used properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
v1 中也有 FormCollection 对象。但更推荐使用类型对象。因此,如果您已经这样做了,请继续这样做。
You had the FormCollection object in v1 as well. But it is more preferred to use a typed object. So if you are already doing that, then continue doing so.
通过使用 FormCollection,您最终会使用字符串类型手动将发布数据或查询字符串键/值匹配到要在代码中使用的值(生成字符串类型代码),而内置的模型绑定可以为您完成此操作如果您使用表单模型,也称为“类型化对象”。
我认为通过使用 FormCollection,您可能还会失去在模型对象上使用方便的数据注释(斜杠验证)属性的能力,这些属性是为与类型化对象模型绑定一起使用而设计的。
此外,一旦您开始接触controller.Request.Form,单元测试就会变得更加麻烦。您可能会发现自己必须模拟并设置 HttpContextBase 和 HttpRequestBase 才能让模拟请求的 .Form 属性返回您希望测试看到的 NameValueCollection。将此与让模型绑定为您完成工作进行对比,例如:
总之,我建议尽量不要使用 FormCollection。
By using FormCollection, you wind up manually matching your post data or query string key/values into values to use in your code using string typing (resulting in stringly-typed code), when instead the built-in Model Binding can do this for you if you use form models, aka "typed objects."
I think by using the FormCollection, you would probably also lose the ability to use the handy Data Annotation (slash Validation) attributes on your model objects as well, which are designed for use with typed object model binding.
Additionally, unit testing can become much more cumbersome once you start touching your controller.Request.Form. You might find yourself having to mock and setup an HttpContextBase, and an HttpRequestBase just to have that mock request's .Form property return the NameValueCollection that you are wanting your test to see. Contrast this to letting model binding do the work for you such as:
In summary, I would recommend against using FormCollection to the maximum extent possible.