在 Razor 视图中仅显示日期
在我的模型中,我有属性:
public DateTime CreatedFrom { get; set; }
在强类型视图中,我如何显示该日期并使其可编辑(但只有日期,而不是时间)。 我使用:
@Html.EditorFor(m=>m.CreatedFrom.ToShortDateString())
当我想转到该视图时发生异常:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
如何正确执行?
In my model i have propery:
public DateTime CreatedFrom { get; set; }
In strongly typed view i what to show that date and make it editable (but only date,not time).
I use:
@Html.EditorFor(m=>m.CreatedFrom.ToShortDateString())
When i want to go to that view exception occures:
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
How do it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linquinze 是对的,EditorFor 用于表达式而不是函数。
如果您尝试为您的日期显示一个文本框,您可以使用:
我相信这会实现相同的效果。
Linquinze is right, the EditorFor is for an Expression and not a Func.
If you are trying to display a textbox though for your date you could use:
which I believe will achieve the same thing.
kamil,您不能将模型绑定到属性方法,您只能将其绑定到:
我强烈建议您在控制器(或服务类)内解决日期时间问题,或者作为初始 HttpGet 操作的一部分填充视图,或者当您发送回值时填充到 HttpPost 中(这种情况不太理想)。无论哪种方式,都必须在控制器级别采取补救措施。
kamil, you cannot bind your model to a property method, you will only be able to bind it to:
I would strongly advise that you sort out the datetime issue inside your controller (or service class) either as part of the initial HttpGet action to populate the view, or inside the HttpPost when you send the values back (not quite as desirable a scenario). Either way, the remedial action must be taken at controller level.
请注意,定义不是,
因此
razor 引擎仅查找字段名 CreatedFrom,但无法处理方法调用。
可能您需要额外的 js 来帮助来回转换。
Note that definition is
not
So the razor engine only looks for field name CreatedFrom, but cannot handle method call.
May be you need additional js to help convert back and forth.