【发布时间】:2018-07-12 09:15:31
【问题描述】:
我很难弄清楚如何为界面类型视图模型呈现显示模板。假设我有一个如下视图模型:
public class DashboardViewModel
{
public ISelectedWalletsViewModel Wallets { get; set; }
}
public interface ISelectedWalletsViewModel
{
}
public class OneSelectedWalletViewModel : ISelectedWalletsViewModel
{
public SelectedWalletViewModel SelectedWallet { get; set; }
public IEnumerable<WalletViewModel> OtherWallets { get; set; }
}
public class AllSelectedWalletsViewModel : ISelectedWalletsViewModel
{
public IEnumerable<WalletViewModel> Wallets { get; set; }
}
public interface IWalletViewModel
{
long Id { get; set; }
string Name { get; set; }
string Description { get; set; }
string CurrentBalance { get; set; }
}
public class WalletViewModel : IWalletViewModel
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string CurrentBalance { get; set; }
}
public class SelectedWalletViewModel : IWalletViewModel
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string CurrentBalance { get; set; }
}
所以我创建了 DisplayTemplates 文件夹来为每个模型设置相应的 .cshtml 文件:
<!-- START OneSelectedWalletViewModel.cshtml -->
@using CLSoft.MyWallet.Models.Home
@model OneSelectedWalletViewModel
@Html.DisplayFor(m => m.SelectedWallet)
@Html.DisplayFor(m => m.OtherWallets)
<a asp-action="Index">Select all wallets</a>
<!-- END OneSelectedWalletViewModel.cshtml -->
<!-- START AllSelectedWalletsViewModel.cshtml -->
@using CLSoft.MyWallet.Models.Home
@model AllSelectedWalletsViewModel
@Html.DisplayFor(m => m.Wallets)
<!-- END AllSelectedWalletsViewModel.cshtml -->
<!-- START SelectedWalletViewModel.cshtml -->
@using CLSoft.MyWallet.Models.Home
@model SelectedWalletViewModel
<div class="card border-primary">
<div class="card-body">
<h5 class="card-title">@Model.Name</h5>
<h6 class="card-subtitle mb-2 text-muted">@Model.CurrentBalance</h6>
<p class="card-text">@Model.Description</p>
</div>
</div>
<!-- END SelectedWalletViewModel.cshtml -->
<!-- START WalletViewModel.cshtml -->
@using CLSoft.MyWallet.Models.Home
@model WalletViewModel
<div class="card">
<div class="card-body">
<h5 class="card-title">@Model.Name</h5>
<h6 class="card-subtitle mb-2 text-muted">@Model.CurrentBalance</h6>
<p class="card-text">@Model.Description</p>
<a asp-action="Index" asp-route-wallet-id="@Model.Id">select wallet</a>
</div>
</div>
<!-- END WalletViewModel.cshtml -->
现在我只缺少控制器操作方法:
[HttpGet]
public async Task<IActionResult> Index(long? walletId)
{
var viewModel = await _service.GetDashboardViewModelAsync(walletId);
return View(viewModel);
}
以及 Index.cshtml 视图代码:
@using CLSoft.MyWallet.Models.Home
@model DashboardViewModel
@{
ViewData["Title"] = "Index";
}
<div class="row">
<div class="col">
<h4>Your wallets</h4>
@Html.DisplayFor(m => m.Wallets)
</div>
</div>
现在我应该准备好了。事情是没有标记被渲染。在我的测试期间,我尝试为 ISelectedWalletsViewModel 界面添加显示模板
@model ISelectedWalletsViewModel
@Html.DisplayForModel()
在@Html.DisplayForModel() 上放一个断点,然后 - 哒哒! - 调试器按原样停止。但是,唉,没有调用具体的 DisplayTemplate。我错过了什么吗?
编辑 1:我创建了一个 github repo 来显示问题。
EDIT 2:我也在AspNet/Mvc repository上报告了这个问题
【问题讨论】:
-
您正在使用
@Html.DisplayFor来显示IEnumerable?另外,在控制器的return行之前添加一个断点并检查viewModel的内容 -
我纠正了错字。我已经检查了 viewModel 的内容,这正是它所需要的。
-
我编辑了代码以反映一个真实的例子
-
另外,我尝试使用 if (Model is OneSelectedWalletViewModel) 检查 Index 视图中的模型类型,它返回了预期的结果。类型似乎没有在 DisplayFor 链中得到评估。
-
我一直在 ASP.NET MVC 5 中使用 EditorFor 和 DisplayFor 的接口渲染具体的 ViewModel。对我来说,这是一个重大的退步。
标签: c# razor asp.net-core asp.net-core-mvc viewmodel