在ajax加载的局部视图中回发时模型为null

时间:2016-09-16 14:19:49

标签: ajax asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 umbraco

我使用以下模式https://github.com/filamentgroup/Ajax-Include-Pattern 通过ajax加载部分视图。

查看:

    @using(Html.BeginUmbracoForm("PostContactInformation", "JoiningSurface", null, new Dictionary<string, object> { { "class", "joinform" } })) {
        @Html.AntiForgeryToken()
        <div data-append="@Url.Action("RenderJoiningContactInformation", "JoiningSurface", new { ContentId = CurrentPage.Id })"></div>
    }

采取行动:

 public ActionResult RenderContactInformation(int ContentId)
       {
            var viewModel = ContactViewModel();
            viewModel.Content = Umbraco.TypedContent(ContentId);

            return PartialView("RenderContactInformation", viewModel);
        }

完美加载局部视图。

//我不需要添加局部视图

邮政行动也正常:

public ActionResult PostContactInformation(ContactViewModel model)  
{
//code here
    return RedirectToUmbracoPage(pageid);
}

问题是,我需要在CurrentUmbracoPage中添加模型错误,如果它存在于帖子中...

例如:

public ActionResult PostContactInformation(ContactViewModel model)  
{
    ModelState.AddModelError(string.Empty, "Error occurred");
    return CurrentUmbracoPage();
}

在这种情况下,我得到当前模型的空值。这只有在我使用ajax时才会发生。

如果我同步加载动作:

   @using(Html.BeginUmbracoForm("PostJoiningContactInformation", "JoiningSurface", null, new Dictionary<string, object> { { "class", "joinform" } })) {
                @Html.AntiForgeryToken()
                @Html.Action("RenderContactInformation", "JoiningSurface", new { ContentId = CurrentPage.Id })
            }

一切都像它应该的那样。

但我需要使用ajax。在这种情况下,是否有正确的方法在回发时传递值?我知道我可以使用TempData,但我不确定这是最好的方法。 谢谢你的耐心

1 个答案:

答案 0 :(得分:1)

问题是,当您尝试通过ajax调用访问Umbraco上下文时,无法访问Umbraco上下文。那些电话有点不同。

在这个帖子中检查我的答案:Umbraco route definition-ajax form我建议使用WebAPI和UmbracoApiControllers在Ajax调用期间访问这些值。