【发布时间】:2022-01-14 15:47:17
【问题描述】:
假设我有以下模型:
public class Parent : BaseEntity
{
public Child? Child { get; set; }
}
public class Child: BaseEntity
{
public string? Name { get; set; }
}
然后我将父模型发送到我的视图:
@model Parent
如果孩子为空,我想显示一个自定义的孩子名。我怎样才能在类似于以下的 1 行中做到这一点?
<input type="text" placeholder="@Model.Child?.Name ?? DefaultName">
【问题讨论】:
-
"@Model.Child?.Name ?? DefaultName"(顺便说一句,你不需要string?,string很好,Child?也一样,因为它是一个引用类型。) -
我想我错过了一些东西。它不工作。 :/ Btw with .NET6 string 变成了一个可为空的类型。
标签: c# html asp.net razor razor-pages