Umbraco - 如何使用剃刀脚本设置属性的值 [英] Umbraco - how to set the value of a property using razor script

查看:47
本文介绍了Umbraco - 如何使用剃刀脚本设置属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常简单的投票表单,我想尝试使用 razor 而不是使用外部控件.

I'm trying to create a very simple polling form and I thought of having a go at it using razor instead of using an external control.

我创建了一个表单,其中列出了一个问题和一个答案列表,当我们按下提交时,我们会转到调用 razor 脚本来处理结果的页面.

I've created a form that lists one question and a list of answers and when we press submit we go to the page that calls the razor script that handles the results.

我想要做的是遍历所有答案并将它们的计数器加一.每个 Answer 对象上都有一个名为Counter"的数字属性.

What I want to do is loop through all the answers and increment their counter by one. There is a numeric property called "Counter" on each Answer object.

然而,这一直失败.如果我这样做:

However that keeps failing. If I do:

var objAnswer = @Model.NodeById(Int32.Parse(submittedAnswer));
objAnswer.getProperty("Counter").Value++;

或类似的方式,它们都失败了.奇怪的是 objAnswer.getProperty("Counter") 确实包含一个数字,但是当我尝试设置它时,我收到此错误:

or similar ways, they all fail. What is weird is that objAnswer.getProperty("Counter") does contain a number, but when I try to set it I get this error:

   umbraco.MacroEngines.DynamicNull' does not contain a definition for 'Value'

我明白了我也试过

Document post = new Document(objAnswer.Id);
post.Publish(user);

但这也失败了.

有没有简单的方法来实现这一目标?

Is there an easy way of achieving this?

答案是这个!!

      Document doc = new Document(objAnswer.Id);
      doc.getProperty("counter").Value = 34;
      umbraco.BusinessLogic.User author = umbraco.BusinessLogic.User.GetUser(0); 
      doc.Publish(author);
      umbraco.library.UpdateDocumentCache(doc.Id);

不是说属性需要小写.

推荐答案

Model.NodeById() 返回一个 DynamicNode 对象,其属性是只读的.您肯定会想要使用 Document 路由,特别是因为 Node 和 DynamicNode 都从 XML 缓存中获取数据,而 Document 操作数据库.

Model.NodeById() returns a DynamicNode object, the properties of which are read only. You'll want to go the Document route for sure, especially since both Node and DynamicNode both get their data from the XML cache, whereas Document manipulates the database.

这篇关于Umbraco - 如何使用剃刀脚本设置属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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