在视图中显示对象 ToString() 值
Displaying object ToString() value in View
我有一个 ASP .Net Core MVC 1.1 网络应用程序。在其中,我有一个视图 - Details.cshtml 仅显示选定的记录:
@model InspectionsData.Models.House
@{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<div>
<dl class="dl-horizontal">
@*<dd>
@Html.DisplayFor(model => model.Id)
</dd>*@
<dt>
@Html.DisplayNameFor(model => model.Street1)
</dt>
<dd>
@Html.DisplayFor(model => model.Street1)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Street2)
</dt>
<dd>
@Html.DisplayFor(model => model.Street2)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Street3)
</dt>
<dd>
@Html.DisplayFor(model => model.Street3)
</dd>
<dt>
@Html.DisplayNameFor(model => model.City)
</dt>
<dd>
@Html.DisplayFor(model => model.City)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Region)
</dt>
<dd>
@Html.DisplayFor(model => model.Region)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Country)
</dt>
<dd>
@Html.DisplayFor(model => model.Country)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PropertyType)
</dt>
<dd>
@Html.DisplayFor(model => model.PropertyType)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>
在上面,"PropertyType" 是一个 class,看起来像这样:
public partial class PropertyType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string PropertyTypeName { get; set; }
public override string ToString()
{
return PropertyTypeName;
}
}
如您所见,我重写了 ToString() 方法,希望 View 在呈现时执行 ToString(),从而显示 PropertyType 的值,但它并没有这样做。所以它只是空白。在我看来,我尝试这样做:
@Html.DisplayFor(model => model.PropertyType.ToString())
但一点都不喜欢(错误:InvalidOperationException:模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式)
我是否必须向我的 PropertyType class 添加一个字符串字段并将视图绑定到该字段以显示 属性 类型?或者是否有我不知道的更优雅的解决方案?
谢谢...
一个选项非常简单:
@Model.PropertyType.ToString()
我有一个 ASP .Net Core MVC 1.1 网络应用程序。在其中,我有一个视图 - Details.cshtml 仅显示选定的记录:
@model InspectionsData.Models.House
@{
ViewData["Title"] = "Details";
}
<h2>Details</h2>
<div>
<dl class="dl-horizontal">
@*<dd>
@Html.DisplayFor(model => model.Id)
</dd>*@
<dt>
@Html.DisplayNameFor(model => model.Street1)
</dt>
<dd>
@Html.DisplayFor(model => model.Street1)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Street2)
</dt>
<dd>
@Html.DisplayFor(model => model.Street2)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Street3)
</dt>
<dd>
@Html.DisplayFor(model => model.Street3)
</dd>
<dt>
@Html.DisplayNameFor(model => model.City)
</dt>
<dd>
@Html.DisplayFor(model => model.City)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Region)
</dt>
<dd>
@Html.DisplayFor(model => model.Region)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Country)
</dt>
<dd>
@Html.DisplayFor(model => model.Country)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PropertyType)
</dt>
<dd>
@Html.DisplayFor(model => model.PropertyType)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>
在上面,"PropertyType" 是一个 class,看起来像这样:
public partial class PropertyType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string PropertyTypeName { get; set; }
public override string ToString()
{
return PropertyTypeName;
}
}
如您所见,我重写了 ToString() 方法,希望 View 在呈现时执行 ToString(),从而显示 PropertyType 的值,但它并没有这样做。所以它只是空白。在我看来,我尝试这样做:
@Html.DisplayFor(model => model.PropertyType.ToString())
但一点都不喜欢(错误:InvalidOperationException:模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式) 我是否必须向我的 PropertyType class 添加一个字符串字段并将视图绑定到该字段以显示 属性 类型?或者是否有我不知道的更优雅的解决方案?
谢谢...
一个选项非常简单:
@Model.PropertyType.ToString()