【问题标题】:How can I get property values from an injected model in _Layout shared view?如何从 _Layout 共享视图中的注入模型中获取属性值?
【发布时间】:2020-09-07 22:38:21
【问题描述】:

由于line,我收到以下错误:

@inject ClaveSol.Models.Category category

...

@foreach (var item in (IEnumerable<Category>)category)
{
    <a class="dropdown-item" asp-asp-area="" asp-controller="Categories" asp-action="Index">
        @Html.DisplayFor(modelItem => item.Name)
    </a>
}
InvalidCastException: Unable to cast object of type 'ClaveSol.Models.Category' to type 'System.Collections.Generic.IEnumerable`1[ClaveSol.Models.Category]'.
AspNetCore.Views_Shared__Layout.<ExecuteAsync>b__38_1() in _Layout.cshtml, line 39

我试图将 Category 模型传递给我的 _Layout.cshtml 视图并迭代它的 Name 属性值。 我关注了this tuto。我在 foreach 中添加了(IEnumerable) 转换(here),因为我得到了CS1579 error

我该如何解决?

编辑:

难道没有其他方法可以在不使用注入的情况下获取类别模型吗?还是这种局部视图的最佳方式?

【问题讨论】:

  • ClaveSol.Models.Category 类别将其更改为 IEnumerable 类别并将 (IEnumerable) 类别中的 var 项更改为类别中的 var 项。希望它应该工作
  • @user13422309 项目在 foreach 中返回 null

标签: c# asp.net-mvc asp.net-core dependency-injection


【解决方案1】:

您将ClaveSol.Models.Category 注入到视图中,它就是class。然后你将一个对象传递给foreach,而不是对象列表。

如果您的模型有一个对象,请不要使用 foreach 并将您的代码替换为:

@inject ClaveSol.Models.Category category


<a class="dropdown-item" asp-asp-area="" asp-controller="Categories" asp-action="Index">
    @Html.DisplayFor(modelItem => category.Name)//<--Note THIS
</a>

或者,如果您的模型是列表,则必须将 @inject 模型更改为此

@inject List<ClaveSol.Models.Category> category

这个link 可能对你有帮助

更新

创建一个这样的界面

public interface ICategoryService
{
    List<Category> GetAllCategories();
}

创建一个这样的类

public class CategoryService : ICategoryService
{
    private readonly ClaveSolDbContext  _dbContext;

    public CategoryService(ClaveSolDbContext  dbContext)
    {
        _dbContext = dbContext;
    }

    public List<Category> GetAllCategories()
    {
        return _dbContext.Category.ToList();
    }
}

ICategoryService注册到ConfigureService类中ConfigureService方法中的DI

service.AddScoped<ICategoryService,CategoryService>();

然后在你的布局中注入ICategoryService

@inject ICategoryService categoryService

最后在foreach中调用GetAllCategories

@foreach (var item in (IEnumerable<Category>)categoryService.GetAllCategories())
{
    <a class="dropdown-item" asp-asp-area="" asp-controller="Categories" asp-action="Index">
        @Html.DisplayFor(modelItem => item.Name)
    </a>
}

【讨论】:

  • 我的模型类别有超过 1 个具有其 name 属性的对象。我尝试了inject List&lt;ClaveSol.Models.Category&gt; category 并在startup.cs 中添加了services.AddTransient&lt;List&lt;Category&gt;&gt;();,但foreach 中的项目返回null。
  • @dlag 您只需将services.AddTransient&lt;List&lt;Category&gt;&gt;(); 注册到DI。您必须为services.AddTransient&lt;List&lt;Category&gt;&gt;(); 注册一个默认值。看到这个link
  • 插入 @model IEnumerable&lt;ToDoItem&gt; 时出现错误 CS0246。后来在教程中,他们在 ConfigureServices() 中插入了 services.AddTransient&lt;IToDoItemRepository, ToDoItemRepository&gt;();,但我不想修改我的 Category 模型,只像在任何其他视图中一样迭代对象。
  • 当我为类和接口插入 services.AddScoped&lt;ICategoryService,CategoryService&gt;(); 时,在 startup.cs 上出现 CS0246 错误。 using ClaveSol.Models; 说没必要。
  • @dlag 是否在startup.cs 中添加ICategoryService 的命名空间?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 2013-11-07
  • 1970-01-01
相关资源
最近更新 更多