【问题标题】:Model is null on postback in ajax loaded partial view模型在 ajax 加载的部分视图中回发时为空
【发布时间】:2017-01-24 19:41:55
【问题描述】:

我正在使用以下模式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,但我不确定这是最好的方法。 感谢您的耐心等待

【问题讨论】:

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


    【解决方案1】:

    问题是当您尝试通过 ajax 调用访问 Umbraco 上下文时,它是不可访问的。这些电话有点不同。

    在此线程中查看我的答案:Umbraco route definition-ajax form,我建议使用 WebAPI 和 UmbracoApiControllers 以便能够在 Ajax 调用期间访问这些值。

    【讨论】:

    • 感谢您的回复,马辛!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多