为什么我会收到这个错误:
模板只能用于字段访问、属性访问、单维数组索引或单参数自定义索引器表达式
在这个代码:
@model IEnumerable<ArtSchoolProject.Models.Trainer>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_PageLayout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<ul class="trainers">
@foreach (var item in Model) {
<li>
<div>
<div class="left">
<a href="@Url.Action("Details", "Details", new { id = item.ID })">
<img src="~/Images/Trainer/@item.Picture" />
</a>
</div>
<div class="right">
@Html.ActionLink(item.Name,"Details",new {id=item.ID})
<br />
@Html.DisplayFor(modelItem=>@string. item.Description.ToString().Substring(0,100))
</div>
</div>
</li>
}
</ul>
网址:
@Html.DisplayFor(modelItem=>item.Description.ToString().Substring(0,100))
更新:
问题解决了,我在代码中加入了:
@{
string parameterValue = item.Description.ToString().Substring(0, 100);
}
@Html.DisplayFor(modelItem=>parameterValue)
我的新代码:
@foreach (var item in Model) {
<li>
<div>
<div class="left">
<a href="@Url.Action("Details", "Details", new { id = item.ID })">
<img src="~/Images/Trainer/@item.Picture" />
</a>
</div>
<div class="right">
@Html.ActionLink(item.Name,"Details",new {id=item.ID})
<br />
@{
string parameterValue = item.Description.ToString().Substring(0, 100);
}
@Html.DisplayFor(modelItem=>parameterValue)
</div>
</div>
</li>
}
这只是一种可能性。出于好奇,还有其他解决这个错误的方法吗?