c# – “模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式”错误
问题:
为什么我收到错误:
模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式
在这段代码:
@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>
}
这只是一种可能性。 只是出于好奇,还有另一种解决错误的方法吗?
Why am I receiving the error:Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressionsat this code:at line:Update:Problem solved.I added to my code :My new code:This is only one possibility.Just for curiosity is there another solution for solving the error?