获取 currentBlock 作为方法中的参数 - EPiServer MVC

Get the currentBlock as parameter in method - EPiServer MVC

您好。我的块有一个控制器,其中包含:

 public class QuestionBlockController : BlockController<QuestionBlock>
    {
        public override ActionResult Index(QuestionBlock currentBlock)
        {
            return PartialView(currentBlock);
        }

        public void Vote(QuestionBlock currentBlock)
        {
              //Do stuff
        }

投票方法是通过 ajax 调用调用的,在该块的视图中可以看到:

@Ajax.ActionLink("Vote", "Vote", "QuestionBlock",
                        new AjaxOptions
                        {
                            HttpMethod = "POST"
                        }, new { @class = "btn btn-vote" })

问题是,当我单击 actionlink 时,它会调用 Vote 方法,但 currentBlock 为 null

如果我调试 currentBlock for Index 方法,它会正确设置为 currentBlock 作为当前 BlockData 对象。

我错过了什么?

此致,

克里斯伦

您应该在操作中提及 currentBlock 参数 link。如果 QuestionBlock 是您从中调用 Ajax.ActionLink 的视图模型,它应该是这样的:

@Ajax.ActionLink("Vote", "Vote", "QuestionBlock",Model,
                        new AjaxOptions
                        {
                            HttpMethod = "POST"
                        }, new { @class = "btn btn-vote" })

和 Model(currentBlock) 将作为参数传递给投票操作。

我得到了将 currentBlock 内容引用作为参数值传递给 ajax request.This 的建议,效果很好。

@Ajax.ActionLink("Vote", "Vote", "QuestionBlock", new {currentBlock = Model.ContentLink,
                        new AjaxOptions
                        {
                            HttpMethod = "POST"
                        }, new { @class = "btn btn-vote" })