{InvalidOperationException: Templates can be used only with field
access, property access, single-dimension array index, or single-parameter
custom indexer expressions}
I recently encountered above exception, when I tried to bind
Data Model to View to show count of Order and here is Code:
HTML View:
<div class="form-group">
@Html.LabelFor(m => m.PostedOrders)
<div class="col-md-3">
@Html.DisplayFor(m =>
m. PostedOrders.Count())
</div>
</div>
If
you see exception, it tell, you can only bind @Html.DisplayFor with Field, Property of Model and it does not
support function or expression.
To fix
this problem, you need to add one additional property or field in Model Class [OrderDataModel]
that holds the total count of posted orders and you can bind this
field/property with view to display total count of posted order
C# :
public class OrderDataModel
{
public List<Order> PostedOrder { get; set; }
public int Total => PostedOrder.Count;
}
HTML View:
<div class="form-group">
@Html.LabelFor(m => m.Total)
<div class="col-md-3">
@Html.DisplayFor(m =>
m.Total)
</div>
</div>
Thanks
for visiting, please leave your comments if it helps
No comments:
Post a Comment