在以前的ASP.NET MVC版本中,您可以找到有关ViewModel的一些信息以及如何在此版本中使用它们.
我想知道为什么我在ASP.NET Core MVC中找不到关于这个主题的任何信息?这个概念是否仍然存在,如果需要,我需要把它们放在哪里?
问题出现了,因为我想为项目制作仪表板.项目是我的网络应用程序的主要入口点.他们有许多关系,例如里程碑.
楷模:
public class Project
{
public int ProjectId { get; set; }
public string Name { get; set; }
public ICollection<Milestone> Milestones { get; set; }
...
}
public class Milestone
{
public int MilestoneId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime Deadline { get; set; }
public int? ParentId { get; set; }
public Milestone Parent { get; set; } …Run Code Online (Sandbox Code Playgroud) 引用此VisualStudioMagazine文章,我试图将代码放在单独的文件中,而不是剃刀视图中。
我试过了:
@page "/Item"
@using WebApplication1.Shared
@using WebApplication1.Client.Services;
@inject HttpClient Http
@inherits ItemComponent
@if (ItemList != null)
{
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Metal</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
@foreach (var item in ItemList)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Category</td>
<td>@item.Metal</td>
<td>@item.Price</td>
<td>@item.Quantity</td>
</tr>
}
</tbody>
</table>
}
@functions{
public ItemModel[] ItemList;
ItemComponent IC = new ItemComponent();
protected override async Task OnInitAsync()
{
ItemList = IC.GetItems().Result;
//ItemList = await Http.GetJsonAsync<ItemModel[]>("api/Item/GetItems");
StateHasChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
和ItemComponent:
using System.Threading.Tasks;
using …Run Code Online (Sandbox Code Playgroud)