【问题标题】:MVC3 Ajax.BeginForm with PartialView and persistent routedata issue具有 PartialView 和持久路由数据问题的 MVC3 Ajax.BeginForm
【发布时间】:2012-05-04 02:39:39
【问题描述】:

我有一个主视图,该视图的 URL 有一个 Action/Controller/Area 和 id 值,类似于:

http://localhost:56513/Incident/IncidentHome/Index/8c02a647-a883-4d69-91be-7ac5f7b28ab7

我在这个主视图中有一个局部视图,它通过 Ajax 调用控制器中的方法。这个局部视图需要知道父页面的 url 的 ID 值。我发现如何做到这一点是通过“ParentActionViewContent”。比如:

        using (Ajax.BeginForm("UpdatePersonalStatusPanel", "Status", new { area = "Tools" , id = ViewContext.ParentActionViewContext.RouteData.Values["id"].ToString() }, new AjaxOptions { UpdateTargetId = "divPersStatus" }))
        {
            <p style="text-align: center;">
                <span class="editor-label">@Html.LabelFor(m => m.StatusText)</span> <span class="editor-field">@Html.EditorFor(m => m.StatusText)</span>
                <input type="submit" value="Change Current Status" />
            </p>
        }

现在,这非常适合调用控制器方法。 ID 被正确传递,以便控制器可以在路由数据中看到它。我使用 id 执行数据库调用,然后再次返回部分视图。问题在于退货。我在 ajax.beginform 的 ViewContext.ParentActionViewContext.RouteData.Values["id"].ToString() 位上得到一个“对象引用未设置为对象的实例”,并且我的 targetid 没有刷新。

显然我一定做错了什么。其他人有更好的方法通过 Ajax 查看父视图的路由数据吗?

【问题讨论】:

    标签: asp.net-mvc-3 jquery asp.net-mvc-routing ajax.beginform


    【解决方案1】:

    如果我对您的理解正确,则此部分视图自称。所以ParentActionViewContext 第一次工作是因为你的主视图第一次使用这个局部视图调用一个动作。但是,稍后的 ajax 调用会直接返回这个局部视图。当直接调用局部视图时,没有父视图操作,因此 ParentActionViewContext 上的空引用。

    我建议在局部视图的模型中包含 id,而不是处理路线数据。

    new { area = "Tools" , id = Model.Id }
    

    【讨论】:

    • 我想这就是要走的路!我只是想,如果我在模型中包含 id,当它已经在 URL 中时,它会是多余的,但是这种方式没有问题,所以谢谢!
    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多