Umbraco razor 模板 - 从参数中指定的字段获取格式化日期
我正在尝试从 umbraco 中的剃刀模板返回格式化日期。我不确定如何从参数中定义的字段获取值。
这是我正在使用的代码。我传入的字段称为“articleDate”。我正在获取参数值输出,但是当我尝试使用参数名称获取字段的值时,它不会返回任何内容。如果我通过字段名称本身请求值,那就有效。如何创建这样的通用宏?
@{var param = @Parameter.dateField;}
Field Name: @param
<br/>
Field Value: @Model.param
<br/>
Field Value: @Model.articleDate
我也尝试使用 @Model.GetDynamicMember(..) ,但这只会引发异常。
Field Value: @Model.GetDynamicMember("articleDate");
Error loading Razor Script getDate.cshtml
Cannot invoke a non-delegate type
有人能指出我正确的方向吗?我只是想创建一个简单的宏,我可以用它来格式化页面上的日期。
是否可以将我的日期值直接传递到剃刀宏中?这就是我目前的称呼:
<umbraco:Macro ID="Macro1" Alias="getDate" dateField="articleDate" runat="server"></umbraco:Macro>
I'm trying to return a formatted date from a razor template in umbraco. I'm not sure how to get a value from a field defined in a parameter though.
Here is the code I'm playing with. The field I'm passing in is called "articleDate". I'm getting the parameter value output, however when I try to get the value of the field using the parameter name it returns nothing. If I ask for the value by the field name itself, that works. How can I create a generic macro like this?
@{var param = @Parameter.dateField;}
Field Name: @param
<br/>
Field Value: @Model.param
<br/>
Field Value: @Model.articleDate
I tried using @Model.GetDynamicMember(..) as well, but that just throws an exception.
Field Value: @Model.GetDynamicMember("articleDate");
Error loading Razor Script getDate.cshtml
Cannot invoke a non-delegate type
Can someone point me in the right direction? I'm just trying to create a simple macro I can use to format dates across my page.
Is it possible to pass the value of my date directly into the razor macro? This is how I'm currently calling it:
<umbraco:Macro ID="Macro1" Alias="getDate" dateField="articleDate" runat="server"></umbraco:Macro>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您像您所写的那样调用宏:
您实际上正在传递字段“articleDate”的名称。在宏中,您可以通过以下方式获取模型的articleDate 属性的值:
除了宏之外,我还建议使用helpers 或 - 对于更复杂的脚本 - RenderPage。可以在这里找到一篇不错的文章:
http: //joeriks.wordpress.com/2011/03/11/better-struct-for-your-razor-scripts-with-renderpage-in-umbraco/
示例:
您可以使用 Page 对象将参数传递给使用 RenderPage 渲染的脚本:
getdate.cshtml:
If you call your Macro like you wrote:
You're actually passing the name of the field "articleDate". In the macro, you then may get the value of the Model's articleDate property by using:
Instead of macro's I also recommend using helpers or - for more complex scripts - RenderPage. A nice writeup can be found here:
http://joeriks.wordpress.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/
Example:
You may pass parameters to scripts rendered with RenderPage by using the Page object:
getdate.cshtml: