asp.net-mvc 如何从 ActionResult 获取模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2071095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the Model from an ActionResult?
提问by Omu
I'm writing a unit test and I call an action method like this
我正在编写单元测试,并调用这样的操作方法
var result = controller.Action(123);
result is ActionResultand I need to get the model somehow, anybody knows how to do this?
结果是ActionResult,我需要以某种方式获得模型,有人知道该怎么做吗?
回答by Mark Seemann
In my version of ASP.NET MVC there is no Actionmethod on Controller. However, if you meant the Viewmethod, here's how you can unit test that the result contains the correct model.
在我的 ASP.NET MVC 版本中Action,Controller 上没有方法。但是,如果您指的是该View方法,那么您可以通过以下方法对结果包含正确的模型进行单元测试。
First of all, if you only return ViewResult from a particular Action, declare the method as returning ViewResult instead of ActionResult.
首先,如果您只从特定的 Action 返回 ViewResult,请将该方法声明为返回 ViewResult 而不是 ActionResult。
As an example, consider this Index action
例如,考虑这个索引操作
public ViewResult Index()
{
return this.View(this.userViewModelService.GetUsers());
}
you can get to the model as easily as this
你可以像这样轻松地进入模型
var result = sut.Index().ViewData.Model;
If your method signature's return type is ActionResult instead of ViewResult, you will need to cast it to ViewResult first.
如果您的方法签名的返回类型是 ActionResult 而不是 ViewResult,您需要先将其转换为 ViewResult。
回答by Max
We placed the following piece in a testsbase.cs allowing for typed models in the tests a la
我们将以下部分放在 testsbase.cs 中,允许在测试中输入模型
ActionResult actionResult = ContextGet<ActionResult>();
var model = ModelFromActionResult<SomeViewModelClass>(actionResult);
ModelFromActionResult...
ModelFromActionResult...
public T ModelFromActionResult<T>(ActionResult actionResult)
{
object model;
if (actionResult.GetType() == typeof(ViewResult))
{
ViewResult viewResult = (ViewResult)actionResult;
model = viewResult.Model;
}
else if (actionResult.GetType() == typeof(PartialViewResult))
{
PartialViewResult partialViewResult = (PartialViewResult)actionResult;
model = partialViewResult.Model;
}
else
{
throw new InvalidOperationException(string.Format("Actionresult of type {0} is not supported by ModelFromResult extractor.", actionResult.GetType()));
}
T typedModel = (T)model;
return typedModel;
}
An example using a Index page and List:
使用索引页面和列表的示例:
var actionResult = controller.Index();
var model = ModelFromActionResult<List<TheModel>>((ActionResult)actionResult.Result);
回答by Omu
consider a = ActionResult;
考虑一个 = ActionResult;
ViewResult p = (ViewResult)a;
p.ViewData.Model
回答by Chris Marisic
It's somewhat cheating but a very trivial way to do so in .NET4
这有点作弊,但在 .NET4 中是一种非常简单的方法
dynamic result = controller.Action(123);
result.Model
Used this today in a unit test. Might be worth some sanity checks such as:
今天在单元测试中使用了它。可能值得进行一些健全性检查,例如:
Assert.IsType<ViewResult>(result);
Assert.IsType<MyModel>(result.Model);
Assert.Equal(123, result.Model.Id);
You could skip the first one if the result is going to be a view or partial result depending on the input.
如果结果将是视图或部分结果(取决于输入),您可以跳过第一个。

