Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templates can be used only with field access, property access, single-dimension array index error

I have a model like this :

 public partial class ClassTime
{
    public int Id { get; set; }
    public int ScheduleId { get; set; }
    public System.TimeSpan StartClassTime { get; set; }
    public System.TimeSpan FinishClassTime { get; set; }
    public string ClassDay { get; set; }

    public virtual Schedule Schedule { get; set; }
}

So with this repository method :

public string ReturnPersianDay(int id)
        {
            if (id == 1) return "شنبه";
            if (id == 2) return "یکشنبه";
            if (id == 3) return "دوشنبه";
            if (id == 4) return "سه شنبه";
            if (id == 5) return "چهارشنبه";
            if (id == 6) return "پنج شنبه";
            if (id == 7) return "جمعه";
            else return "نامشخص";
        }

in my view i call this method as you can see here :

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ScheduleId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StartClassTime)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FinishClassTime)
        </td>
        @{EducationRepositor.ClassTimeRepository objclasstime=new ClassTimeRepository();
          
          }
        <td>
       @Html.DisplayFor(modelItem => objclasstime.ReturnPersianDay(int.Parse(item.ClassDay)))        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
} 

But i get this error :

 Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. 

The stack trace :

[InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.]
   System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData, ModelMetadataProvider metadataProvider) +556
   System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData) +54
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +68
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +104
   System.Web.Mvc.Html.DisplayExtensions.DisplayFor(HtmlHelper`1 html, Expression`1 expression) +61
   ASP._Page_Views_ClassTime_Index_cshtml.Execute() in e:\Projects migration to Qazvin\ziafat\EducationModel\EducationMVC\Views\ClassTime\Index.cshtml:45
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +103
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +245
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +22
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +176
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +75
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +99
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
like image 476
Ehsan Akbar Avatar asked May 15 '14 13:05

Ehsan Akbar


2 Answers

You can't use DisplayFor because the expression can't leverage that extension method, you just use the raw value:

@objclasstime.ReturnPersianDay(int.Parse(item.ClassDay))

instead of:

@Html.DisplayFor(modelItem => objclasstime.ReturnPersianDay(int.Parse(item.ClassDay)))
like image 80
Mike Perrenoud Avatar answered Nov 18 '22 00:11

Mike Perrenoud


If you want to use a method with DisplayFor, just store the value in a variable before using it.

You had

@Html.DisplayFor(modelItem => objclasstime.ReturnPersianDay(int.Parse(item.ClassDay)))

Instead, try this

var persianDay = bjclasstime.ReturnPersianDay(int.Parse(item.ClassDay));
@Html.DisplayFor(m => persianDay)
like image 2
Daniel Avatar answered Nov 18 '22 01:11

Daniel