.NET Core 2.0视图组件未填充@Model属性

时间:2018-10-09 20:43:43

标签: c# razor asp.net-core-2.0

我正在学习如何使用.NET Core 2.0,而我需要使用的其中一部分就是视图组件。

我几乎完全想通了。我正在使用依赖注入将服务传递到ViewComponent构造函数中,然后将TestItem列表传递给视图。

@model System.Collections.Generic.List<Common.TestItem>属性始终为nullViewContext.ViewData.Model正确填充。

任何想法为何?代码在下面发布。


服务

TestItemService

public class TestItemService
    {
        public List<TestItem> GetItems()
        {
            return new List<TestItem>()
            {
                new TestItem(){ id=0, date=DateTime.Today.AddDays(-2), Name="TestItem1" },
                 new TestItem(){ id=1, date=DateTime.Today.AddDays(-4), Name="TestItem2" },
                  new TestItem(){ id=2, date=DateTime.Today.AddDays(3), Name="TestItem3" },
            };
        }
    }

依赖注入

Startup方法的ConfigureServices类中:

services.AddScoped<TestItemService.TestItemService, TestItemService.TestItemService>();

ViewComponent

public class TestViewComponent : ViewComponent
{
    private readonly TestItemService.TestItemService service;
    public TestViewComponent(TestItemService.TestItemService context)
    {
        service = context;
    }


    public async Task<IViewComponentResult> InvokeAsync()
    {
        List<TestItem> items = await GetItemsAsync();
        return View("Default",items);
    }
    public Task<List<TestItem>> GetItemsAsync()
    {
        return service.GetItems().Where(i => i != null).ToAsyncEnumerable().ToList();
    }
}

查看

@page
@model System.Collections.Generic.List<Common.TestItem>




<h3>Priority Items</h3>
<ul>
    @foreach (var todo in (List<Common.TestItem>)ViewContext.ViewData.Model)
    {
        <li>@todo.Name</li>
    }
</ul>

1 个答案:

答案 0 :(得分:1)

我发现了问题。在我的view组件cshtml文件中,我在顶部有一个 elseif (ext == '.s3p') %Read in file to be analyzed; convert to string format so %that the strsplit function can be used inFile = textread(thisFile, '%s', 'delimiter', '\n'); %#ok<DTXTRD> strForm = string(inFile); cellOut = arrayfun(@(x) strsplit(x, ' '), strForm, 'UniformOutput', false); %Find the end of the meta data and where the Option line of %the .s3p file begins (denoted by a '#'). Convert Option %line to string for parsing. metaDataEndLine = find(~cellfun(@isempty, strfind(inFile, 'Measurements'))) - 1; %#ok<STRCLFH> opLine = find(~cellfun(@isempty, strfind(inFile, '#'))); %#ok<STRCLFH> opStr = cellstr(cellOut{opLine}); %Generate meta-data character strings to be printed to the %output file. File format can be generalized such that each %column corresponds to spefied data unit (i.e. frequency is %always the second col). metaFreq = opStr(1,2); metaParam = opStr(1,3); metaFormat = opStr(1,4); metaResist = opStr(1,6); %Genereate a cell array that houses the up-front data %provided by the file. Remove unnecessary chars. metaDataPrintFile = strForm(1:metaDataEndLine); metaDataPrintFile = cellfun(@(x)erase (x, '!'), metaDataPrintFile, 'UniformOutput', false); metaDataPrintFile = cellfun(@(x)erase (x, 'Correction: '), metaDataPrintFile, 'UniformOutput', false); %Add data to the class array metaPrint. Append string %variables generated above. this.metaPrint = metaDataPrintFile; this.metaPrint{length(this.metaPrint) + 1} = strcat('Freq = ', metaFreq); this.metaPrint{length(this.metaPrint) + 2} = strcat('Parameter = ', metaParam); this.metaPrint{length(this.metaPrint) + 3} = strcat('Format = ', metaFormat); this.metaPrint{length(this.metaPrint) + 4} = strcat('Input Resistance = ', metaResist); end %if 标记,由于某种原因,该标记被破坏了。如果其他人会解释 为什么 导致问题的答案,我会将其更改为其他人的可接受答案。 @page标签的作用是什么