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 expressions
at this code:
@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>
at line:
@Html.DisplayFor(modelItem=>item.Description.ToString().Substring(0,100))
Update:
Problem solved. I added to my code :
@{
string parameterValue = item.Description.ToString().Substring(0, 100);
}
@Html.DisplayFor(modelItem=>parameterValue)
My new code:
@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>
}
This is only one possibility. Just for curiosity is there another solution for solving the error?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…