模板只能用于字段访问,属性访问,单维数组索引错误

时间:2014-05-15 13:36:02

标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

大家好我有这样的模特:

 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; }
}

使用此存储库方法:

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 "نامشخص";
        }

在我看来,我可以在这里看到这个方法:

@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>
} 

但是我收到了这个错误:

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

堆栈跟踪:

[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

2 个答案:

答案 0 :(得分:6)

您无法使用DisplayFor,因为该表达式无法利用该扩展方法,您只需使用原始值:

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

而不是:

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

答案 1 :(得分:2)

如果您想使用DisplayFor的方法,请在使用之前将值存储在变量中。

你有

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

相反,试试这个

var persianDay = bjclasstime.ReturnPersianDay(int.Parse(item.ClassDay));
@Html.DisplayFor(m => persianDay)