找一个模板错误,当我尝试这样做? [英] Get a template error when I try to do this?

查看:147
本文介绍了找一个模板错误,当我尝试这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net MVC 3,我不断收到这个错误,因为我没有使用一个模板,我不明白。

我有这在我的局部视图

  @model ViewModels.FormViewModel    < D​​IV =标签-1>
        @ Html.TextBoxFor(X => x.Due.ToShortDateString())
    < / DIV>

在我的视图模型

 公共类FormViewModel
    {
        公众的DateTime由于{搞定;组; }
        公共FormViewModel()
        {
            DUEDATE = DateTime.UtcNow;
        }
    }

和我得到这个错误


  

模板只能与现场使用
  访问属性访问,
  一维数组索引,或者
  单参数自定义索引
  前pressions。说明:未处理
  期间发生的异常
  当前Web请求的执行。
  有关详情,请堆栈跟踪
  有关该错误以及信息
  它起源于code。


  
  

异常详细信息:
  System.InvalidOperationException:
  模板可以只用现场使用
  访问属性访问,
  一维数组索引,或者
  单参数自定义索引
  前pressions。



解决方案

应该是这样的:

  @ Html.TextBoxFor(X => x.Due)

如果你想在这个日期一些格式为:

  [DisplayFormat(DataFormatString ={0:YYYY-MM-DD},ApplyFormatInEditMode = TRUE)]
公众的DateTime由于{搞定;组; }

和则:

  @ Html.EditorFor(X => x.Due)

如果你真的想用这个 .ToShortDateString()方法,你需要使用非强类型的辅助(显然这件事情我不推荐):

  @ Html.TextBox(由于,Model.Due.ToShortDateString())

I am using asp.net mvc 3 and I keep getting this error and I don't understand it since I am not using a template.

I have this in my partial view

@model ViewModels.FormViewModel

    <div="tabs-1">
        @Html.TextBoxFor(x => x.Due.ToShortDateString())
    </div>

in my viewmodel

public class FormViewModel
    {
        public DateTime Due { get; set; }


        public FormViewModel()
        {
            DueDate = DateTime.UtcNow;         
        }
    }

and I get this error

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

解决方案

Should be like this:

@Html.TextBoxFor(x => x.Due)

And if you want some format for this date:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Due { get; set; }

and then:

@Html.EditorFor(x => x.Due)

of if you really want to use this .ToShortDateString() method you need to use a non-strongly typed helper (obviously that's something I would recommend against):

@Html.TextBox("Due", Model.Due.ToShortDateString())

这篇关于找一个模板错误,当我尝试这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆