【发布时间】:2011-10-04 03:58:40
【问题描述】:
在 MVC2 中,我曾经以一种在发布时从未使用过 FormCollection 对象的方式创建强类型视图。我的签名总是这样:
[AcceptVerbs(HttpVers.Post)]
public Create(Person newPerson)
{
//code to update the person from the post
}
但现在我看到了这种新的 TryUpdateModel 方式,我只想写如下内容:
[AcceptVerbs(HttpVers.Post)]
public Create()
{
Person thePersonToCreate = new Person()
TryUpdateModel(thePersonToCreate)
{
//Code to create the person if model is valid
}
}
所以现在看来我必须模拟 HTTPContext 才能测试此方法。但是,似乎我仍然可以使用前一种方式使用强类型方法。我意识到 TryUpdateModel 方法对于那些会使用 FormCollection 方法做事的人来说是一种改进,但为什么要打扰 TryUpdateModel 呢?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2