简体   繁体   English

如何在MVC .net核心中访问返回控制器的字段结果

[英]How to access field result from returned controller in MVC .net core

Whilst my controller functions correctly, I am getting a syntax error in my unit test on the "Assert" line for .Name虽然我的控制器功能正常,但我在单元测试中的“Assert”行中出现语法错误.Name

IAction result does not contain a definition for "Name"... IAction 结果不包含“名称”的定义...

If I hover over "result" in debug mode I can see the data is in the result variable (Result > Model > Name).如果我在调试模式下将鼠标悬停在“结果”上,我可以看到数据在结果变量中(结果 > 模型 > 名称)。 I've tried accessing it with Result.Model.Name but that's a syntax error also.我试过用 Result.Model.Name 访问它,但这也是一个语法错误。

Unit test:单元测试:

    [Fact]
    public async Task TestGetNameById()
    {
        string expectedName = "Component";

        using (var context = GetContextWithData())
        using (var controller = new AssetTypesController(context))
        {
            var result = await controller.Details(2);
            Assert.Equal(expectedName, result.Name);
        }
    }

Controller action:控制器动作:

   public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var assetType = await _context.AssetType
            .SingleOrDefaultAsync(m => m.AssetTypeId == id);
        if (assetType == null)
        {
            return NotFound();
        }

        return View(assetType);
    }

Model模型

public class AssetType
{
    [DatabaseGenerated(databaseGeneratedOption: DatabaseGeneratedOption.Identity)]
    [Key]
    public int AssetTypeId { get; set; }
    [Required]
    public string Name { get; set; }
}

That's because IActionResult is not a type of AssetType .这是因为IActionResult不是一个类型的AssetType

Try something like this:尝试这样的事情:

[Fact]
public async Task TestGetNameById()
{
    string expectedName = "Component";

    using (var context = GetContextWithData())
    {
        var controller = new AssetTypesController(context);
        var result = await controller.Details(2) as ViewResult;
        var assetType = (AssetType) result.ViewData.Model;
        Assert.Equal(expectedName, assetType.Name);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 6 MVC:如何从 controller 访问 Program.cs 中定义的变量值? - ASP.NET Core 6 MVC : how to access a variable's value that is defined in Program.cs from a controller? 如何从 ASP.NET mvc 中的 controller 访问 HttpContext - How to access HttpContext from controller in ASP.NET mvc 从ASP.NET Core MVC中的另一个视图访问一个controller的变量 - Access variable of one controller from another view in ASP.NET Core MVC MVC controller API 访问 Blazor 不与 Z303CB0EF9EDB9082D61BBBE5825D972 一起使用 - MVC controller API access in Blazor not working with .NET Core 3.0 如何从 .net 核心 mvc 应用程序中的区域 controller 重定向到根 controller 操作方法? - How to redirect to root controller action method from an area controller in .net core mvc application? 如何从 ASP.NET Core 6 MVC EF 中的 controller 中的另一个 controller 实例化服务? - How do I instantiate a service from another controller in a controller in ASP.NET Core 6 MVC EF? 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC ASP .NET Core MVC Entity Framework - 如何访问由多对多关系产生的表? - ASP .NET Core MVC Entity Framework - How to get access to a table which is result of many-to-many relationship? ASP.NET MVC-如何在Webapi控制器返回的Razor视图中显示数据 - ASP.NET MVC - How to display data in Razor view returned from Webapi controller ASP.NET MVC:如何在Controller加载时将Controller返回的变量打印到View? - ASP.NET MVC: How to print a variable returned from a Controller to the View on page load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM