Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC RC1中从ViewResult获取模型数据?_C#_Asp.net Mvc_Unit Testing - Fatal编程技术网

C# 如何在ASP.NET MVC RC1中从ViewResult获取模型数据?

C# 如何在ASP.NET MVC RC1中从ViewResult获取模型数据?,c#,asp.net-mvc,unit-testing,C#,Asp.net Mvc,Unit Testing,给定以下控制器类: public class ProjectController : Controller { public ActionResult List() { return View(new List<string>()); } } 我曾尝试进入控制器操作,查看设置了哪些内部字段,但没有成功 我对ASP.NET MVC的了解非常有限,但我的猜测是,我没有使用正确的上下文设置控制器 有什么建议吗?在Asp.Net Mvc framew

给定以下控制器类:

public class ProjectController : Controller
{
    public ActionResult List()
    {
        return View(new List<string>());
    }
}
我曾尝试进入控制器操作,查看设置了哪些内部字段,但没有成功

我对ASP.NET MVC的了解非常有限,但我的猜测是,我没有使用正确的上下文设置控制器


有什么建议吗?

在Asp.Net Mvc framework的候选发布版本中,模型通过ViewResult对象的“model”属性提供。以下是更准确的测试版本:

[Fact]
public void List_Action_Provides_ProjectCollection()
{
    //act
    var result = controller.List();

    //assert
    var viewresult = Assert.IsType<ViewResult>(result);
    Assert.NotNull(result.ViewData.Model);
    Assert.IsType<List<string>>(result.ViewData.Model);
}
[事实]
公共作废列表\u操作\u提供\u项目集合()
{
//表演
var result=controller.List();
//断言
var viewresult=Assert.IsType(结果);
Assert.NotNull(result.ViewData.Model);
IsType(result.ViewData.Model);
}
试试:


希望这能有所帮助。

fsabau,你说得绝对正确。真不敢相信我错过了。哦!它说动作结果不包含ViewData的定义
[Fact]
public void List_Action_Provides_ProjectCollection()
{
    //act
    var result = controller.List();

    //assert
    var viewresult = Assert.IsType<ViewResult>(result);
    Assert.NotNull(result.ViewData.Model);
    Assert.IsType<List<string>>(result.ViewData.Model);
}
result.ViewData.Model